Mongoose:不推荐使用mpromise(mongoose' s默认承诺库)

时间:2017-03-08 01:13:31

标签: node.js mongodb express mongoose

我正在使用来自互联网的热门修补程序,但它仍然无法运行......这是我的代码:

var Lobby = require('../models/lobbies-model');
var mongoose = require('mongoose');

mongoose.Promise = require('bluebird');
mongoose.connect('localhost:27017/mydb');
var lobbies = [
    new Lobby({
        nickname: 'Nickname1',
        status: "Status1",
    }),
    new Lobby({
        nickname: 'Nickname2',
        status: "Status2",
    })
];

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

function exit() {
    mongoose.disconnect();
}

我的控制台和结果出错...

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

如何修复该错误?

1 个答案:

答案 0 :(得分:2)

如此处https://github.com/Automattic/mongoose/issues/4291#issuecomment-230312093所述,解决方法是在连接前使用mongoose.Promise = global.Promise;

  mongoose.Promise = global.Promise; // ADD THIS
  mongoose.connect('localhost:27017/mydb');