data.forEach(function(shelf){
books.find({"shelfid":shelf.id},function(book){
//book1,book2,book3
})
});
每个书架都有一个id,我找到所有有这些id的书。 所以假设这个方法找到3本书,我如何回应所有这些 在foreach完成的同时书籍。
答案 0 :(得分:1)
async module非常适合:
它基本上遍历每一个,并允许您指定一个函数在它们全部运行后运行。
var arr = [];
async.each(data, function (fd, callback) {
books.find({"shelfid":fd.id},function(book){
arr.push(book);
callback();
});
}, function(err){
// this runs once they've all been iterated through.
// if any of the finds produced an error, err would equal that error
// books are in the arr array now.
});
正如WiredPrairie指出的那样,这样做更有意义:
books.find({shelfid: { $in: [shelfIds] }}, function(books){
// do stuff with books.
})