JavaScript Nodejs for-loops and wait until loop finished nodejs loop wait,nodejs loop wait for callback,nodejs while

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:

  1. Get all users from mongodb with mongoose
  2. With the help of the lodash each-function go through all users
  3. On each user go through the users books (also with lodash and each)
  4. 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

Answer:1

I finaly soved this problem with the async library with this pattern:

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
           });
Answer:2

In my web app, running in an android WebView, whenever I click on something, or navigate somewhere, that container div is highlighted blue. Sometimes only for a moment, but sometimes until you click ...

In my web app, running in an android WebView, whenever I click on something, or navigate somewhere, that container div is highlighted blue. Sometimes only for a moment, but sometimes until you click ...

  1. android highlight webview
  2. android webview highlight text
  3. android webview highlight search text
  4. android webview highlight selected text
  5. android webview highlight color

I'm just wondering whether it is at all possible to transfer a directory from a unix server to my local machine using the ssh2 module in node.js. I have connected to the remote host and can read the ...

I'm just wondering whether it is at all possible to transfer a directory from a unix server to my local machine using the ssh2 module in node.js. I have connected to the remote host and can read the ...

  1. transfer entire directory scp
  2. transfer whole directory
  3. ftp transfer entire directory
  4. ftp command line transfer entire directory
  5. ftp command to transfer entire directory

Well, I have some function that invokes via ng-click. For example, this function set variable a to true, and after few seconds variable should become false. Function looks like this one: $scope.do =...

Well, I have some function that invokes via ng-click. For example, this function set variable a to true, and after few seconds variable should become false. Function looks like this one: $scope.do =...

  1. make word clock
  2. make right click work on mac
  3. how to make clickshare work
  4. how to make clickbank work
  5. how to make clickshare work on mac
  6. make mouse double click work

I have combed through the sp namespace docs and not found much to go on. I found this snippet from http://www.c-sharpcorner.com/Blogs/12134/how-to-get-the-list-content-types-using-csom-in-sharepoint-...

I have combed through the sp namespace docs and not found much to go on. I found this snippet from http://www.c-sharpcorner.com/Blogs/12134/how-to-get-the-list-content-types-using-csom-in-sharepoint-...

  1. list content type
  2. list content type sharepoint
  3. list content type in sharepoint 2013
  4. list content type http
  5. list content type sharepoint online
  6. list content type id
  7. list content types powershell
  8. list content type java
  9. list content type information
  10. list content-type header
  11. list content type read only
  12. list content types drush
  13. list content type remove column
  14. list content type item
  15. list content type xml
  16. get list content type sharepoint c#
  17. sharepoint list content type id
  18. sharepoint list content type not updated
  19. get list content type sharepoint powershell
  20. sharepoint list content type form