node.js开发错误——DeprecationWarning: Mongoose: mpromise

来源:互联网 发布:星际淘宝主微盘 编辑:程序博客网 时间:2024/05/23 07:22

使用mongoose进行数据库操作时,总是提示:

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


解决方法:

[javascript] view plain copy
  1. var db = mongoose.connect("mongodb://127.0.0.1:27017/mongooseTest");  
之前加一句:

[javascript] view plain copy
  1. mongoose.Promise = global.Promise;  

最后:

[javascript] view plain copy
  1. mongoose.Promise = global.Promise;  
  2. var db = mongoose.connect("mongodb://127.0.0.1:27017/mongooseTest");  

这样就好了。

http://mongoosejs.com/docs/promises.html


Plugging in your own Promises Library

New in Mongoose 4.1.0

While mpromise is sufficient for basic use cases, advanced users may want to plug in their favorite ES6-style promises library like bluebird, or just use native ES6 promises. Just set mongoose.Promise to your favorite ES6-style promise constructor and mongoose will use it.

Mongoose tests with ES6 native promises, bluebird, and q. Any promise library that exports an ES6-style promise constructor should work in theory, but theory often differs from practice. If you find a bug, open an issue on GitHub

    var query = Band.findOne({name: "Guns N' Roses"});    // Use native promises    mongoose.Promise = global.Promise;    assert.equal(query.exec().constructor, global.Promise);    // Use bluebird    mongoose.Promise = require('bluebird');    assert.equal(query.exec().constructor, require('bluebird'));    // Use q. Note that you **must** use `require('q').Promise`.    mongoose.Promise = require('q').Promise;    assert.ok(query.exec() instanceof require('q').makePromise);  


阅读全文
0 0
原创粉丝点击