0

変数「stitcheBook」の変数の値を生成する関数を備えたモジュールがあります。コールバックを使用して、この値を表示および使用できます。

ただし、この値をモジュール プロパティとして使用できるようにしたいと考えています。どうすればこれを達成できますか?

注: _BookStitcher.stitchAllStories 関数の出力を _BookStitcher.stitchedBook プロパティに入れたいと思います。

module.exports = _BookStitcher = (function() {

var db = require('../modules/db');
var stitchedBook = {};

var stitchAllStories = function(callback) {


    db.dbConnection.smembers("storyIdSet", function (err, reply) {
        if (err) throw err;
        else {
            var storyList = reply;
            console.log(storyList);
            // start a separate multi command queue
            multi = db.dbConnection.multi();
            for (var i=0; i<storyList.length; i++) {
                multi.hgetall('story/' + String(storyList[i]) + '/properties');
            };
            // drains multi queue and runs atomically
            multi.exec(function (err, replies) {
                stitchedBook = replies;
                // console.log(stitchedBook);
                callback(stitchedBook);
            });
        };
    });


};


return {
    stitchedBook : stitchedBook,
    stitchAllStories: stitchAllStories

}

})();

編集:追加する:私は実際にこのようなことをすることで外部から値を設定できることを知っています;

_BookStitcher.stitchAllStories(function (reply) {
        console.log("Book has been stitched!\n\n")
        console.log("the Book is;\n");
        console.log(reply);
        _BookStitcher.stitchedBook = reply;
        console.log("-------------------------------------------------------------------------\n\n\n");
        console.log(_BookStitcher.stitchedBook);

});

_BookStitcher モジュール自体の内部からそれを行う方法があるかどうか疑問に思っていました。

4

1 に答える 1

1

JavaScript でオブジェクト参照がどのように機能するかを利用して、それをプロパティに割り当てることができます

module.exports = _BookStitcher = (function() {

    var db = require('../modules/db');

    // CHANGE HERE
    var stitched = { book: null };

    var stitchAllStories = function(callback) {
        db.dbConnection.smembers("storyIdSet", function (err, reply) {
            if (err) throw err;
            else {
                var storyList = reply;
                console.log(storyList);
                // start a separate multi command queue
                multi = db.dbConnection.multi();
                for (var i=0; i<storyList.length; i++) {
                    multi.hgetall('story/' + String(storyList[i]) + '/properties');
                };
                // drains multi queue and runs atomically
                multi.exec(function (err, replies) {
                    // CHANGE HERE
                    stitched.book = replies;
                    // console.log(stitchedBook);
                    callback(replies);
                });
            };
        });
    };

    return {
        stitched : stitched,
        stitchAllStories: stitchAllStories
    };

}());

そのため、 の中に入れるのではなく、 に入れ_BookStitcher.stitchedBookます_BookStitcher.stitched.book

しかし、それはひどいように見えます。私は決して使用しません。値がいつ利用可能になるかはわかりません。設定されていることが確実な場合にのみ、コールバックから使用するのが安全です。

于 2013-01-31T20:59:10.163 に答える