简体   繁体   中英

I am trying to setup my mongoDB database using mongoose, but i getting an error “mongoose default promise library is deprecated”

I am trying to set up my MongoDB database using mongoose but getting a deprecated warning "Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html "

Here is my code for server.js file below:

var mongoose = require('mongoose');
mongoose.promise = require('bluebird');


// mongoose.promise = global.promise;
mongoose.connect('mongodb://localhost:27017/TodoApp');

var Todo = mongoose.model('Todo', {
  text: {
    type: String
  },
  completed: {
    type: Boolean
  },
  completedAt: {
    type: Number
  }
});

var newTodo = new Todo({
  text: 'Cook dinner'
});

newTodo.save().then((doc) => {
  console.log('Saved todo', doc);
}, (e) => {
  console.log('Unable to save todo')
});

I already tried to install bluebird and to use it as my third party promise but still getting the same error message.

try using this code mate

var mongoose = require('mongoose');

mongoose.Promise = global.Promise;
mongoose.connect('mongodb://10.7.0.3:27107/data/TodoApp'); 

You should use

mongoose.Promise = require('bluebird');

You are using

mongoose.promise = require('bluebird');

Also I think you are using older version of nodejs. I use node js 8 & mongoose takes global.Promise by default as nodejs 8 comes with native Promise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM