In Meteor framework, I thought that using Promise.Await is meant exactly to do that – to wait for the promise to be resolved. However, in the following code, the console logs Promise:
'projects.count.categories': function () {
if (Meteor.isServer) {
try {
let result = Promise.await(Projects.rawCollection().aggregate(
[
{
$match: { }
},
{
$group: { _id: "$category", count:{$sum:1} }
}
]
));
console.log(result.toArray()) //RETURNS PENDING
return result;
}
catch (e) {
throw e;
}
}
},
I have not found what is wrong with the code in this case.
>Solution :
toArray is what returns a promise (e.g. see this question). You’d need to do:
Promise.await(Projects.rawCollection().aggregate(/* ... */).toArray())