我尝试在我的应用程序中使用聚合函数,并从这个调用中捕捉返回值。在
我使用:
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);
});
有什么提示吗?也许是时间问题?
您正在定义一个异步函数
async function getListSuites()
您正在函数中定义一个常量
const testdata =
您没有从函数返回任何东西。
所以当你这么做的时候
var optionString = await getListSuites();
在函数末尾添加一个
null
async function bazbal() {
return {yea:'baby'};
}
async function foobar() {
const bar = await bazbal();
return bar;
}
async function run() {
var optionString = await foobar();
console.log(optionString);
var optionString = await foobar();
console.log(optionString);
};
run();