These are my code.
var myOrderIds = req.user.orders; // array of order document's ObjectId
var myReserv = []; // I want to put specific order into myRerv[] by mongoose findOne()
var showReservList = function(oids, callback) {
oids.forEach(function(oid) {
database.OrderModel.findOne({'_id' : oid, 'isCompleted' : false},function(err, docs) {
callback(docs);
});
});
}
showReservList(myOrderIds, function (res) {
myReserv.push(res);
});
console.log(myReserv); // empty
The problem is myReserv[] is empty.
I wonder how to consolidate multiple asynchronous processes and not just handle the completion of a single one. (This is the common case.)
console.log(myReserv)
is executed faster than myRserv.push(res)
. I want to delay console.log(myRresrv)
until myRserv.push(res)
finishes to work.