Mongoose default promise library deprecated on MEAN stack

I try to start the MEAN stack server, but I get this msg error:

Mongoose: mpromise (the default mongoose promise library) is deprecated, instead add your own promise library: http://mongoosejs.com/docs/promises.html

I tried to find a few answers here, but the one I found was unclear for me:

(node: 3341) Cancel: Warning: Mongoose: mpromise

I found the file calling mongoose.connect, but the codes on this issue didn’t work for me, can anyone explain to me how this works?

+6
source share
4 answers

use this code before connecting mongo and this will solve the promise problem.

mongoose.Promise = global.Promise; 
+10
source

As usual, I connect to MongoDB, use the Bluebird promise library. Read more about this in this post . In any case, this snippet below will help you get started, as this is what I use when prototyping.

 let mongoose = require('mongoose'); let promise = require('bluebird'); let uri = 'mongodb://localhost:27017/your_db'; mongoose.Promise = promise; let connection = mongoose.createConnection(uri); 
+2
source

The latest mongoose library, do not use the default promise library. And from Mongoose v 4.1.0 you can connect your own library.

If you are using the mongoose library (not the underlying MongoDB driver) , then you can mount the promise library as follows:

 //using Native Promise (Available in ES6) mongoose.Promise = global.Promise; //Or any other promise library mongoose.Promise = require('bluebird'); //Now create query Promise var query = someModel.find(queryObject); var promise = query.exec(); 

If you are using the MongoDB driver , you will need to make extra efforts. Because mongoose.Promise sets the promise that the mongoose is not using a driver. You can use the code below in this case.

 // Use bluebird var options = { promiseLibrary: require('bluebird') }; var db = mongoose.createConnection(uri, options); 
+2
source

Work for me.

Mongoose v4.11.7 solve the promise problem

 const mongoose = require('mongoose'); mongoose.Promise = global.Promise; mongoose.connection.openUri('mongodb://127.0.0.1:27017/app_db', { /* options */ }); 

Mongoose #save ()

 var article = new Article(Obj); article.save().then(function(result) { return res.status(201).json({ message: 'Saved message', obj: result }); }, function (err) { if (err) { return res.status(500).json({ title: 'Ac error occurred', error: err }); } }); 
+1
source

All Articles