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
2 Answers
Reset to default 8There are a couple of mistakes in the code.
- You are calling
callback(null, outputArray)
in the wrong place. It should be right after the innermostif
condition async.each
final callback takes only one argument -err
, soresults
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
版权声明:本文标题:javascript - How to get the result of async.each in Node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741520757a2383168.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论