0

我有一个名为 top thread 的帖子数组,我正在尝试检索一个数组数组,每个数组包含对原始数组中单个帖子的所有回复,而不是最好的。这是我获取顶级线程的代码

exports.topThread = function(req, res){
// gets the leaf id from URL and assigns to OPID
var OPID = req.params.id;
var thread = [];
// finds the post with id OPID from the db
titles.findOne({postID: OPID}, function (err, post) {
if (err) throw err; 


getBestThread(OPID, thread, function(result){

    branchList.getBranches(function(branches){

        // renders content with data retrieved from the post
        res.render('content', {title: post.title, content: post.body, OPID: post.postID, currentThread: result, branches: branches});
        console.log(result);

    });
    });
});

};

这部分代码有效:结果是我想要的帖子数组。现在我想将结果数组的每个元素传递给我的 getAllOtherReplies 函数

//calback to retrieve other replies for a parent comment
    getOtherReplies = function(parentID, callback){
        postdb.otherReplies(parentID, function(err, commentsArray){
            if (err) throw err;
            callback(commentsArray);
        })
    }

    getAllOtherReplies = function(threadArray, returnArray, callback) {
        async.each(threadArray, 
            function(value, callback) {
                getOtherReplies(value.commentID, function(commentsArray) {
                    console.log(value.commentID);
                    if (commentsArray)
                        returnArray.push(commentsArray);
                });
                callback();
            },
            function(err) {
                if (err) throw err;
            }
        );
        callback(returnArray);
    }

这是postsDB中的函数:

//retrieves other replies from db
    exports.otherReplies = function(parentID, callback){

        titles.find({type: "comment", parentID: parentID}).sort({hotness: -1}).toArray(function(err, result){
            if (err) throw err;
            if (result.length > 1)
                callback(result.splice(0,1));
            else callback(null);
        });
    };

现在我尝试修改我的原始代码以调用 getAllOtherReplies 函数:

    exports.topThread = function(req, res){
        // gets the leaf id from URL and assigns to OPID
        var OPID = req.params.id;
        var thread = [];
        var allOtherReplies = [];
        // finds the post with id OPID from the db
        titles.findOne({postID: OPID}, function (err, post) {
        if (err) throw err; 


        getBestThread(OPID, thread, function(result){
            getAllOtherReplies(result, allOtherReplies, function(returnArray){
                console.log(returnArray);

                    branchList.getBranches(function(branches){

                    // renders content with data retrieved from the post
                    res.render('content', {title: post.title, content: post.body, OPID: post.postID, currentThread: result, branches: branches});
                    console.log(result);

                });
            });
            });
        });
    };

并且不是返回结果中每个评论的所有回复的数组数组,而是返回一个数组数组,其中每个数组仅是对结果中第一个评论的回复,重复 n 次,其中 n 是结果中的帖子数。我知道我缺少一个异步问题,任何帮助将不胜感激。

4

1 回答 1

1

在您的async.each循环内部,getOtherReplies正在添加commentsArray回调内部,但是您在之后立即调用异步回调getOtherReplies而不是等待它的回调。

将异步回调移入getOtherReplies将是一个好的开始,并且可能很好地解决问题。

getOtherReplies(value.commentID, function(commentsArray) {
    console.log(value.commentID);
        if (commentsArray) {
            returnArray.push(commentsArray);
        }
        callback();
    });
于 2013-02-18T21:28:27.853 回答