我有以下代码:
//Marks all users which are reading the book with the bookId
var markAsReading = function (bookId,cb) {
User.find({}, function (err,users) {
if(err)
cb(err);
//Go through all users with lodash each function
_(users).each(function (user) {
//Go through all books
_(user.books).each(function (book) {
if(book.matchId === bookId)
{
user.isReading = true;
//cb();
}
});
});
//Need to callback here!!#1 cb(); -->Not working!
});
//Or better here! cb() --> Not working
};
exports.markAsReading = markAsReading;
我正在使用带有mongoose和mongodb的nodejs。 我想做什么:
我的问题是我只需要在位置#2完成所有内容时回调 但是整个User.find及其嵌套的回调都没有准备好!
如果所有循环和find方法都准备就绪,我如何解决这个问题呢?
我已经阅读了有关promises和async lib的内容,但我如何在这种情况下使用它?
最诚挚的问候 迈克尔
答案 0 :(得分:5)
我终于用这种模式的异步库解决了这个问题:
async.forEach(list,function (item,callback) {
//do something with the item
callback();//Callback when 1 item is finished
}, function () {
//This function is called when the whole forEach loop is over
cb() //--> This is the point where i call the callback because the iteration is over
});
答案 1 :(得分:0)
您可以使用来自灵活http://caolan.github.io/nimble/
的每个循环进行同步var nimble = require('nimble');
var markAsReading = function (bookId,cb) {
User.find({}, function (err,users) {
if(err)
cb(err);
nimble.each(users, function (user) {
nimble.each(user.books, function (book) {
if(book.matchId === bookId)
{
user.isReading = true;
}
});
});
cb(null);
});
};