2013-02-18 44 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(); 
    });