0

I'm getting this error message when running node file.js in the Mac terminal. My terminal doesn't have any special config. I tried adding "mongoose.Promise = global.Promise" before "mongoose.connect...".

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

Here's the source code:

var Product = require('../models/product');

var mongoose = require('mongoose');

mongoose.Promise = global.Promise;

mongoose.connect('localhost:27017/shopping');

var products = [
    new Product({
        imagePath: 'https://upload.wikimedia.org/wikipedia/en/5/5e/Gothiccover.png', 
        title: 'Gothic 5 Video Game',
        description: 'Awesome Game!!!!',
        price: 10
    }),
    new Product({
        imagePath: 'https://upload.wikimedia.org/wikipedia/en/5/5e/Gothiccover.png', 
        title: 'Gothic 4 Video Game',
        description: 'Also Awesome Game!!!!',
        price: 20
    }),
    new Product({
        imagePath: 'https://upload.wikimedia.org/wikipedia/en/5/5e/Gothiccover.png', 
        title: 'Gothic 3 Video Game',
        description: 'best game ever!!!!',
        price: 15
    }),
    new Product({
        imagePath: 'https://upload.wikimedia.org/wikipedia/en/5/5e/Gothiccover.png', 
        title: 'Gothic 2 Video Game',
        description: 'top notch!!!',
        price: 50
    })
];

var done = 0;
for (var i = 0; i < products.length; i++) {
    products[i].save(function(err, result){
        done++;
        if (done === products.length) {
            exit();
        }
    });
}

function exit() {
    mongoose.disconnect();
}
veron
  • 61
  • 3
  • 9

2 Answers2

3

The error says that Mongoose's default promise library is deprecated, plug in your own promise library instead.

By default, when you use Mongoose, it has a default promise library plugged into it. It is using mpromise which comes with Mongoose.

So, it has a default implementation of the promise library, but, for whatever reason, they do not want you to use it any more.

Instead, you have a couple of options of using your own promise library. The most popular ones in the Node.js community are Bluebird, Q, or ES6 Promises.

I recommend you go with ES6 Promises because it is going to absolutely fulfill everything that you need for promises coming out of Mongoose.

If this seems a bit arbitrary, like why doesn't Mongoose just figure this out for us. Mongoose does this to give us more flexibility on what promise implementation we want to use, which for some users, it's really important. For some projects, it's something that you are going to care about, but, when you are first starting out with this, you don't really worry about it much.

So, all you have to do is fix up Mongoose's internal promise library:

At the very top of your file where you have this:

const mongoose = require('mongoose');

Underneath of it you are going to write:

mongoose.Promise = global.Promise;

So, on the left hand side we are saying hey Mongoose, whenever you want to create a promise for any kind of interaction with the database whatsoever, we want you to use this implementation of the promise library.

global.Promise is a reference to the ES6 implementation of promises inside the Node.js environment.

So, we are saying with this code, whenever you need to implement a promise just use the ES6 implementation.

halfer
  • 19,824
  • 17
  • 99
  • 186
Daniel
  • 14,004
  • 16
  • 96
  • 156
  • [@halfer](https://stackoverflow.com/users/472495/halfer) I see your edit's date, at this time we have mongoose 6? How does it behave is you don't add mongoose.Promise = global.Promise? I haven't used it and it seems to be working well. Just curious... Thanks – Julio Spinelli Oct 21 '21 at 14:22
0

I was having the same problem. To get rid of the warning, you have to add "mongoose.Promise = global.Promise" to every file where you've required mongoose.

Paul Hiers
  • 61
  • 1