2013-02-18 46 views
0

我有一个称为顶级线程的帖子数组,我试图检索一个数组数组,每个数组包含对原始数组中单个帖子的所有答复都是最好的。这里是我得到顶线的代码使用异步库的控制流程

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); 

    }); 
    }); 
}); 

};

这部分代码的工作原理: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); 

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

,而不是返回在结果每个评论的所有答复的数组的数组和,它返回一个数组的数组,其中每个阵列是replie s仅对结果中的第一条评论,重复n次,其中n是结果中的帖子数。我知道我错过了一个异步问题,任何帮助将不胜感激。

回答

1

里面你async.each循环,getOtherReplies是增加commentsArray回调里面,但你getOtherReplies后立即调用异步回调,而不是等待它的回调。

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

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