我正在尝试在像这样的 _.each lodash 函数中在 Mongoose 查询中完成同步-
let commentContainer = [];
let comments = {..} //json object having comments
_.each(comments, comment => {
User.findOne({_id: comment.createdBy}).exec()
.then(function(commentor){
var c = {
text: comment.text,
votes: comment.votes.length,
commentor: {
name: commentor.name,
profilePhoto: commentor.profilePhoto,
id: commentor._id
}
}
commentContainer.push(c);
});
});
}
console.log(commentContainer); //it shows []
我该如何实现它,我尝试通过延迟使用 setTimeout 函数,但它似乎不是一个有效的过程。