admin管理员组

文章数量:1290981

I'm trying to fill outputArray and then send it as a JSON. When I add console.log() after outputArray.push I can see that outputArray is being filled. But in the bottom function, it prints an empty array. I see that the bottom function works before the array is filled. How can I modify below code so that I can receive the full array. Thanks in advance.

      var outputArray = [];

      async.each(generalArr, function(item, callback)
      {
          var docs =  collection.find({ id: { "$in": item}}).toArray();


        docs.then(function(singleDoc)
        {
          if(singleDoc)
          {


              outputArray.push.apply(outputArray, singleDoc);
          }
        });
        callback(null, outputArray);

    }, function(err,results)
      {
      if (err) 
        return console.log('ERROR', err);

      console.log(results);
      res.json(results);


      }
  ) 

I'm trying to fill outputArray and then send it as a JSON. When I add console.log() after outputArray.push I can see that outputArray is being filled. But in the bottom function, it prints an empty array. I see that the bottom function works before the array is filled. How can I modify below code so that I can receive the full array. Thanks in advance.

      var outputArray = [];

      async.each(generalArr, function(item, callback)
      {
          var docs =  collection.find({ id: { "$in": item}}).toArray();


        docs.then(function(singleDoc)
        {
          if(singleDoc)
          {


              outputArray.push.apply(outputArray, singleDoc);
          }
        });
        callback(null, outputArray);

    }, function(err,results)
      {
      if (err) 
        return console.log('ERROR', err);

      console.log(results);
      res.json(results);


      }
  ) 
Share Improve this question edited Jul 25, 2017 at 11:18 jason asked Jul 25, 2017 at 8:53 jasonjason 7,16438 gold badges125 silver badges209 bronze badges 8
  • Possible duplicate of How do I return the response from an asynchronous call? – str Commented Jul 25, 2017 at 9:08
  • @str none of the answers are about Node.js. And they are not related with this question. – jason Commented Jul 25, 2017 at 9:10
  • The same concepts also apply to Node.js (it is JavaScript after all). You seem to have nested async functions of which the inner changes data but you expect it to be available in the outer. If this is what you want, then yes the linked question is related. – str Commented Jul 25, 2017 at 9:54
  • @str I canceled the first async. But still I can't get the value of outputArray out of the loop.. And I really couldn't find a solution in the linked question. :( – jason Commented Jul 25, 2017 at 11:09
  • Then please update (and properly indent) your code. – str Commented Jul 25, 2017 at 11:10
 |  Show 3 more ments

2 Answers 2

Reset to default 8

There are a couple of mistakes in the code.

  1. You are calling callback(null, outputArray) in the wrong place. It should be right after the innermost if condition
  2. async.each final callback takes only one argument - err, so results is not available

The following code should work...

var outputArray = [];

async.each(generalArr, function(item, callback) {
    var docs =  collection.find({ id: { "$in": item}}).toArray();
    docs.then(function(singleDoc) {
        if(singleDoc) {
            outputArray.push.apply(outputArray, singleDoc);
        }
        callback();
    });
}, function(err) {
    if (err) return console.log('ERROR', err);

    console.log(outputArray);
    res.json(outputArray);
});

I'm assuming that catch code is also there for the DB promise query, else the final callback might never be called.

Don't use async.js when plain promises are so much easier:

Promise.all(generalArr.map(function(item) {
  return collection.find({id: {"$in": item}}).toArray();
})).then(function(outputArray) {
  var results = outputArray.filter(Boolean);
  console.log(results);
  res.json(results);
}, function(err) {
  console.log('ERROR', err);
});

本文标签: javascriptHow to get the result of asynceach in NodejsStack Overflow