node.js - Not sure how to return endpoint -
i have node/express api not sure how return results of $group.
my code:
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'} }} ]) })
how return results?
i tried doing "return" before "profile.aggregate" function.
i though maybe this:
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; } })
but nothing...
if not wrong aggregation returns promise, can try way
var profile=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.then((data) => { res.status(200).jsonp({ data }); }) .catch(err => { res.status(422).jsonp({errors: err}); });
or can use cursor
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'} }} ]).toarray(function(err, results) { if(!err) res.status(200).jsonp(agenodes); else res.status(422).jsonp(err); });
Comments
Post a Comment