I want to perform a mongodb request, wait until it end and return a result.
The issue is the instruction after the mongo request looks like to be executed before the request end or even start.
users_opposite = collection.find( {"mode" : opposite} ).forEach(function (usersOpposite) {
users_closed.push(usersOpposite);
console.log(users_closed.length);
});
console.log(users_closed.length);
console.log("test");
As a result I'm getting this
0 test 1 2 3 4
How can I say to the following instruction to wait until the foreach is complete ?
Thanks for your help.