Mongoose with Bluebird promisifyAll - saveAsync on model object results in an Array as the resolved promise value Mongoose with Bluebird promisifyAll - saveAsync on model object results in an Array as the resolved promise value mongoose mongoose

Mongoose with Bluebird promisifyAll - saveAsync on model object results in an Array as the resolved promise value


Warning: This behavior changes as of bluebird 3 - in bluebird 3 the default code in the question will work unless a special argument will be passed to promisifyAll.


The signature of .save's callback is:

 function (err, product, numberAffected)

Since this does not abide to the node callback convention of returning one value, bluebird converts the multiple valued response into an array. The number represents the number of items effected (1 if the document was found and updated in the DB).

You can get syntactic sugar with .spread:

person.saveAsync().spread(function(savedPerson, numAffected) {    //savedPerson will be the person    //you may omit the second argument if you don't care about it    console.log(JSON.stringify(savedPerson));}).catch(function(err) {    console.log("There was an error");})


Why not just using mongoose's built-in promise support?

const mongoose = require('mongoose')const Promise = require('bluebird')mongoose.Promise = Promisemongoose.connect('mongodb://localhost:27017/<db>')const UserModel = require('./models/user')const user = await UserModel.findOne({})// ..

Read more about it:http://mongoosejs.com/docs/promises.html