我有一个Node/Express API,我不知道如何返回$组的结果。不知道如何返回端点
我的代码:
router.route('/viewed_card/:invite_id')
.get(function(req, res){
Profile.aggregate([
{$unwind:'$contacts'},
{$unwind:'$contacts.shared'},
{$match:{'contacts.shared.invite_id':req.params.invite_id}},
{$group:{
_id:null,
first_name:{$first:'$contacts.first_name'},
last_name:{$first:'$contacts.last_name'}
}}
])
})
如何返回结果?
我试着在“Profile.aggregate”函数之前做一个“返回”。
我虽然也许是这样的:
router.route('/viewed_card/:invite_id')
.get(function(req, res){
Profile.aggregate([
{$unwind:'$contacts'},
{$unwind:'$contacts.shared'},
{$match:{'contacts.shared.invite_id':req.params.invite_id}},
{$group:{
_id:null,
first_name:{$first:'$contacts.first_name'},
last_name:{$first:'$contacts.last_name'}
}}
], function(err, result){
return result;
}
})
但没有......
在回调中,你应该写'res.send(result)' – zeronone
如果你的运行时支持它,你也可以使用async await。 –