I tried to use a aggregate function in my app and to catch the return values from this call. Everything is fine until I make a console.log(cases);
inside the exec
, but I fail to get the values back to a function as return value.
I use:
async function getListSuites() {
const testdata = await Suite.aggregate([
{
$sort: {
identifier: 1,
last: 1
}
},
{
$group: {
_id: "$identifier",
data: {
$last: {
last: "$last",
_id: "$_id"
}
},
count: {$sum: 1}
}
},
//Optional
{
$project:{
_id: "$data._id",
date: "$data.last",
name: "$_id",
count: "$count"
}
}
]).exec((err, cases) => {
if (err) throw err;
console.log(cases); //works
return cases ;
});
};
router.get('/jsonsuites', async function(req, res){
var optionString = await getListSuites();
console.log(optionString);
res.send(optionString);
});
Any hint? Maybe a timing issue?