I have the following code:
    //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;
I'am using nodejs with mongoose and mongodb. What i want to do:
- Get all users from mongodb with mongoose
- With the help of the lodash each-function go through all users
- On each user go through the users books (also with lodash and each)
- if the current bookId matches the bookId in the function parameter --> Set the book "isReading" property -> true
My problem is that i only need to callback when everything is finished on position #2 But then the whole User.find and its nested callbacks are not ready!
How can i solve this that i do the callback if all loops and the find methods are ready?
I have read something about promises and the async lib but how can i use it in this scenario?
Best Regards Michael
 
     
    