我有一個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。 –