Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 733
Hari Aakash Avatar asked Jan 30 '17 13:01

Hari Aakash


1 Answers

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.

like image 89
Anton Novik Avatar answered Oct 20 '22 04:10

Anton Novik