nocalis
nocalis

Reputation: 176

Async loop with mongodb nodejs

I have 2 collections in MongoDb : users and images

users is like :

{
"_id": ObjectID("58299fc14705d47ff852f8ae"),
"mail": "[email protected]",
"username": "Qwerty1",
"token": "g8m7h0phrvhp774cw0xh9qkt9rhu062r2b3gigm0t1o22zkt9",
 ...,
"images": [
    ObjectID("582c547bb7c5cc391e5a1793"),
    ObjectID("582c54ecb7c5cc391e5a1794")
]
}

images is like :

{
"_id": ObjectID("582c5394b522cb37d701dbe3"),
"user": "g8m7h0phrvhp774cw0xh9qkt9rhu06
"image": base64 encoded
}

I make a find in users to get ids of this user's images and I want to fill an array ( b64Images ) with a a find request to images collection.

db.collection("users").find({$or : [{ username: req.params.username }, {token : req.params.username}]}).toArray(function (err, result) {
   if(result.length){
      var b64Images = [];
      async.each(result[0].images,
      function(item, callback){
         db.collection("images").find({_id : item}).toArray(function (err, resulta) {
            console.log(resulta[0].user);
            b64Images.push(resulta[0].user);
         });
         callback();
      },
      function(err){
         console.log("finished");
         res.json({images : b64Images});
      }
   }
}

But my problem is that my loop is finished before my find in images's response.

So I have an empty array.

I know this is an asynchronous problem but I can't figure it out.

Upvotes: 1

Views: 1136

Answers (1)

oKonyk
oKonyk

Reputation: 1476

I would use Promise.all() approach.

Way it works is: you collect all your promises into array, than pass that array into Promise.all

You can start with this:

//this would go instead of async.each(result[0].images...
let queryPromises = [];

for (let image in result[0].images){
    // toArray returns Promise if no callback passed
    let queryPromise = db.collection("images").find({_id : item}).toArray();
    //query execution has not started yet, so loop is working fine
    queryPromises.push(queryPromise);
}

queryPromises
    // queries execution starts now
   .then(arrayOfResults => { 
      // do stuff with your results here; results arrive all at once
      console.log(arrayOfResults);
   }, err => {
      console.log(err)
   });

Upvotes: 1

Related Questions