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);
}
)