Hari Aakash
Hari Aakash

Reputation: 122

Mongoose Promise error

This is the error which is still being thrown when saving even after adding native promise.

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

mongoose.Promise = global.Promise;
mongoose.connect('mongodb://127.0.0.1/optimusCP')
    .then(function () {
        console.log('Connected to MONGOD !!');
    }).catch(function (err) {
        console.log('Failed to establish connection with MONGOD !!');
        console.log(err.message);
    });

I have tried both bluebird & q, still haven't found a fix for this. Below is the code when I save this, the following deprecation warning shows up..

var user = new User();
        user.email = req.body.email;
        user.password = hash;
        user.save()
            .then(function (user) {
                console.log(user);
            })
            .catch(function (err) {
                console.log(err);
            });

This error is happening on the new version of mongoose which is 4.8.1, but working fine on 4.7.6 mongoose version.

Upvotes: 5

Views: 8327

Answers (4)

daniel
daniel

Reputation: 545

Upgrading Mongoose from 4.8.1 to 4.9.1 solved my problem.

Upvotes: 0

Anton Novik
Anton Novik

Reputation: 1837

Despite using mongoose.Promise = global.Promise; before mongoose.connect(...), I had the same warning.

I discovered, that I initialized mongoose connection in one file:

import mongoose from 'mongoose';

...

// Connect to MongoDB
mongoose.Promise = global.Promise;
mongoose.connect(mongoUri, mongoOptions);
mongoose.connection.on('error', (err) => {
  console.error(`MongoDB connection error: ${err}`);
  process.exit(1);
});

But I imported mongoose in another file too (where mongoose scheme was described), so I added mongoose.Promise = global.Promise; in second file too, as a result of it, the warning disappeared.

import mongoose, { Schema } from 'mongoose';
mongoose.Promise = global.Promise;

const UserSchema = new Schema({ ... });

May be you have the same case.

Upvotes: 4

kailash yogeshwar
kailash yogeshwar

Reputation: 834

I have used bluebird for using promise with mongoose model functions node v6.9.4 :

mongoose.Promise = require('bluebird');

Upvotes: 0

Edward Smith
Edward Smith

Reputation: 552

I've had success getting rid of the message with this

mongoose.Promise = Promise;

Upvotes: 0

Related Questions