Ricardo Alves
Ricardo Alves

Reputation: 77

async.each not processing mongoose find function

I have the following code:

    async.each(arrayUser, function (user, callback) { //loop through array
      console.log('1');
      async.each(user.parentOf, function (child, callback1) { //loop through array
        console.log(2);
        models.Students.find({
            '_id': child._id
          },
          function (err, foundStudent) {
            console.log('3');
            callback1();
          },
          function (err) {
            console.log("InnerLoopFinished");
            callback();
          });
      }, function (err) {
        console.log("OuterLoopFinished");
        console.log('Process Finished');
      });
    });

The arrayUser is created like this:

    var arrayUser = [];
    users.forEach(function (user) {
      var arrayUser1 = {
        parent: {
          _id: user._id,
          firstName: user.firstName,
          lastName: user.lastName
        },
        userRoleID: user.userRoleID.length,
        parentOf: user.parentOf

      };
      arrayUser.push(arrayUser1);
    });

and a sample of user.parentOf is:

    user.parentOf = [{
        studentLastName: 'Flores',
        studentFirstName: 'Aedan',
        studentID: 111444,
        _id: 5a596979ea2d7a360c75948c
      },
      {
        studentLastName: 'Moses',
        studentFirstName: 'Chase',
        studentID: 111756,
        _id: 5a596979ea2d7a360c759489
      }
    ]

My issue is that even with the async.each The function is still not running correctly. It seems to be hitting a wall when it gets to the models.Students.find function and it runs the next callback.

I need to run the InnerLoop completely before moving on to the next user. I followed the correct answer at nodejs Async.each Nested loop Confusion but with no results. I have tried changing the models.Students.find function to findOneAndUpdate and I got the same result.

I think I need to add that I need to find multiple users with an inner array in each. If I had only one user (without the outer loop) it works fine.

my console.log is: 1, 2, 1, 2, 1, 2 ... 3, 3, 3...

and I need it to be 1, 2, 3, 1, 2, 3.

Upvotes: 2

Views: 292

Answers (1)

Akhilesh krishnan
Akhilesh krishnan

Reputation: 799

we need to use async.eachSeries() instead of async.each(). This is the working snippet as per your expectation 1, 2, 3, 1, 2, 3.

async.eachSeries(arrayUser, function (user, callback) { //loop through array
      console.log('1');
      async.eachSeries(user.parentOf, function (child, callback1) { //loop through array
        console.log(2);
        models.Students.find({
            '_id': child._id
          },
          function (err, foundStudent) {
            console.log('3');
            callback1();
          });
      },function (err) {
        console.log("InnerLoopFinished");
        callback();
      });
    }, function (err) {
        console.log("OuterLoopFinished");
        console.log('Process Finished');
      });
    });

this is because the eachSeries function executes in iteratee function in order (after completion of the previous iteration and waits till callback() is called) so it gives output as per your expectation

Upvotes: 6

Related Questions