I have two callback functions from mongoose that I would like to chain using bluebird's then
My first callback function uses then
successfully.
User.findOne().distinct(('Friends.id'), {id: req.body.myId}, {Friends: {$elemMatch: { gender: req.body.gender}}})
.then(function(IDs){
var results = //////some computation
})
.catch(function(error)) {
}
I just can't get the syntax right to chain the second callback function so that it shares the first callback function's catch method. In this case, I cannot use Promise.all
because the second callback function is dependent on the first callback function's results
. Anyway the second callback function is the following:
User.find({Friends: { $not: { $elemMatch: { id: req.body.myId }}}, id: {$in: results}}, function(err, users){
})
You can chain two promises like this.
User.findOne().distinct(('Friends.id'), {id: req.body.myId}, {Friends: {$elemMatch: { gender: req.body.gender}}})
.then(function(IDs){
var results = //////some computation
// second promise
return User.find({Friends: { $not: { $elemMatch: { id: req.body.myId }}}, id: {$in: results}})
})
.then(function(friends) {
// do something with the result of the second query
})
.catch(function(error)) {
}