Node.js 使用异步库控制流

Node.js 使用异步库控制流,node.js,asynchronous,Node.js,Asynchronous,我有一个名为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

我有一个名为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是结果中的帖子数。我知道我缺少一个异步问题,希望您能提供帮助。

在您的
async中。每个
getotherrepries
循环都在回调中添加
commentsArray
,但是您在
getotherrepries
之后立即调用异步回调,而不是等待回调

将异步回调移到
getotherreplays
将是一个良好的开端,可能很好地解决了这个问题

getOtherReplies(value.commentID, function(commentsArray) {
    console.log(value.commentID);
        if (commentsArray) {
            returnArray.push(commentsArray);
        }
        callback();
    });