I've been reading documentaion and articles and everyone seems to describe a different way about using Mongoose and Bluebird together. Even the official Mongoose documentation says something and Bluebird documentaion says another thing.
According to Mongoose:
mongoose.Promise = require('bluebird');
According to Bluebird:
var Promise = require("bluebird");
Promise.promisifyAll(require("mongoose"));
So to my understanding, if you pick the Mongoose way a sample query would be like:
User.findById('someId')
.then(function(){
// do stuff
})
.catch(function(err){
// handle error
})
But also in Mongoose docs it says that:
Mongoose queries are not promises. However, they do have a .then() function for yield and async/await. If you need a fully-fledged promise, use the .exec() function.
So in this case, is .then
above a promise or not?
If you go with Bluebird way:
User.findById('someId')
.execAsync()
.then(function(){
// do stuff
})
.catch(function(err){
// handle error
})
Or maybe even skip execAsync()
and start with findByIdAsync
instead.
Really confused with different documentaion. I'd appreciate if someone can shed some light into this.
Copyright Notice:Content Author:「Özgür Uysal」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40551550/what-is-the-correct-way-of-using-bluebird-for-mongoose-promises