exports.getComments=function(query,skip,limit,callback){
Comment.find(query).sort({create_time:-1}).skip(skip).limit(limit).exec(function(error,comments){
if(error){
callback(error,null);
}
comments.foreach(function(index,comment){
Comment.find({reply_ID:comment.comment_ID}, function(err, replys){
comment.reply=replys;
});
});
callback(error,comments);
})
我的想法是将comment增加一个reply属性,然后再返回comments集合,但由于Comment.find是异步的,故向各位大虾求助,提供点解决方案的,初学,求照顾
11 回复
我的解决思路:
Comment.find(query).sort({create_time:-1}).skip(skip).limit(limit).exec(function(error,comments){
if (comments.length === 0) {
return callback(error, []);
}
var proxy = new EventProxy();
var done = function () {
return callback(error, comments);
};
proxy.after('reply', comments.length, done);
comments.forEach(function(comment,index){
Comment.find({reply_ID:comment.comment_ID},function(error,replys){
comment.reply=replys;
proxy.trigger('reply',replys);
})
})
});
感觉应该可以吧