7

I'm trying to start a MEAN-stack server, however I'm getting this error msg:

Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html

I tried to search some answers here but the one that I found wasn't clear enough for me:

(node:3341) DeprecationWarning: Mongoose: mpromise

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

KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
  • What did you try and what exactly didn't work about it? – JohnnyHK Aug 30 '16 at 14:12
  • I just followed the Mongodb tutorial and the MEAN-stack tutorial, but the server didn't sarts. When I try to access the //localhost:3000/ this errors show in my terminal, and the server never starts. – Joao Luiz Magalhaes Aug 30 '16 at 14:16
  • This message isn't an error, it's just a warning. So the problem is likely someplace else. – JohnnyHK Aug 30 '16 at 14:18
  • So how can I find it? Since it's the only msg that the terminal shows me? just followed the tutorials and it doesn't works. – Joao Luiz Magalhaes Aug 30 '16 at 14:23
  • 1
    KARTHIKEYAN.A has a correct answer to your problem. It will suppress the warning you are seeing. You could follow Wuriyanto if you wanted to use an external promise library like bluebird or q. It depends on how much you care about using deprecated code/methods. @Joao Luiz Magalhaes If something else isn't working you should post a snippet of code. – allegory Mar 13 '17 at 05:01

4 Answers4

11

use this code,before the mongo connection and this will resolve the promise problem.

mongoose.Promise = global.Promise;
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
2

The way I usually connect to MongoDB is by using the Bluebird promise library. You can read more about it in this post. With any luck, this snippet below will help you get started, as it 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);
Community
  • 1
  • 1
Wuriyanto
  • 362
  • 3
  • 6
2

Latest mongoose library, do not use any default promise library. And from Mongoose v 4.1.0 you can plug in your own library.

If you are using mongoose library(not underlying MongoDB driver) then you can plug in promise library like this:

//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 MongoDB Driver then you will need to do some extra effort. Because, mongoose.Promise sets the Promise that mongoose uses not the driver. You can use the below code in this case.

// Use bluebird
var options = { promiseLibrary: require('bluebird') };
var db = mongoose.createConnection(uri, options);
Tolsee
  • 1,695
  • 14
  • 23
  • Thanks Tolsee, i've made this question some time ago, since them, i didnt had to work with mongo, I'll start a new class about Mongo next week, so if I got this trouble again, I'll check your answer. – Joao Luiz Magalhaes Sep 19 '17 at 11:41
2

Work for me.

Mongoose v4.11.7 resolve 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
        });
    }
});
bamossza
  • 3,676
  • 1
  • 29
  • 28