Answer the question
In order to leave comments, you need to log in
How to solve the problem (NodeJS, Mongodb)?
Good afternoon!
I often encounter this problem: I need to find all the documents by ObjectId that are contained in some array, but the response is sent before the loop starts.
node.js, express
var list = [];
for (var i = 0; i < result.items.length; i++) {
Mongo.Items.findOne({'_id': ObjectId(result.items[i])}, function(err3, result3) {
if (err3) return console.log(err3), response.sendStatus(500);
if (result3) list.push(result3)
});
}
return response.send(list); // интерпретатор сразу отправляет результат (пустой массив list), обходя цикл for
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question