I am trying to get results from a series of queries on mongodb using async method but unfortunately the object that I generate is undefined outside the async function scope.
let dataObject = {
prop1: 1,
prop2: 2
....
};
let result = {};
_.each(dataObject, async function (val, key) {
let query = [
{
$match: {
x:1
....
}
}
];
let = MyModel.aggregate(query);
let data = await q.exec();
if (data.length > 0) {
result[key] = data[0].myProp;
console.log(result); // I can see result here
}
});
console.log('====>>', result); // Here the result is undefined
What I am missing?