[英]Mongoose's default promise library is deprecated in MEAN stack
I'm trying to start a MEAN-stack server, however I'm getting this error msg: 我正在尝试启动MEAN堆栈服务器,但是却收到此错误消息:
Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html 猫鼬:mpromise(猫鼬的默认诺言库)已过时,请插入您自己的诺言库: http ://mongoosejs.com/docs/promises.html
I tried to search some answers here but the one that I found wasn't clear enough for me: 我尝试在此处搜索一些答案,但发现的答案对我来说还不够清楚:
(node:3341) DeprecationWarning: Mongoose: mpromise (节点:3341)弃用警告:猫鼬:mpromise
I found the file calling the mongoose.connect, but the codes on that issue didn't work for me, can anyone explain for me how it works? 我找到了名为mongoose.connect的文件,但是关于该问题的代码对我不起作用,有人可以为我解释它的工作原理吗?
在mongo连接之前使用此代码,这将解决promise问题。
mongoose.Promise = global.Promise;
The way I usually connect to MongoDB is by using the Bluebird promise library. 我通常连接到MongoDB的方式是使用Bluebird Promise库。 You can read more about it in this post . 您可以在这篇文章中了解更多信息 。 With any luck, this snippet below will help you get started, as it is what I use when prototyping. 运气好的话,下面的代码片段将帮助您入门,因为这是我在进行原型制作时所使用的。
let mongoose = require('mongoose');
let promise = require('bluebird');
let uri = 'mongodb://localhost:27017/your_db';
mongoose.Promise = promise;
let connection = mongoose.createConnection(uri);
Latest mongoose library, do not use any default promise library. 最新的猫鼬库,请勿使用任何默认的Promise库。 And from Mongoose v 4.1.0 you can plug in your own library. 从Mongoose v 4.1.0起,您可以插入自己的库。
If you are using mongoose library(not underlying MongoDB driver) then you can plug in promise library like this: 如果您使用的是Mongoose库(不是底层的MongoDB驱动程序),则可以插入Promise库,如下所示:
//using Native Promise (Available in ES6) mongoose.Promise = global.Promise; //Or any other promise library mongoose.Promise = require('bluebird'); //Now create query Promise var query = someModel.find(queryObject); var promise = query.exec();
If you are using MongoDB Driver then you will need to do some extra effort. 如果您使用的是MongoDB驱动程序,则需要付出额外的努力。 Because, mongoose.Promise
sets the Promise that mongoose uses not the driver. 因为, mongoose.Promise
设置了mongoose不使用驱动程序的Promise。 You can use the below code in this case. 在这种情况下,您可以使用以下代码。
// Use bluebird var options = { promiseLibrary: require('bluebird') }; var db = mongoose.createConnection(uri, options);
Work for me. 为我工作。
Mongoose v4.11.7 resolve the promise problem 猫鼬v4.11.7解决承诺问题
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connection.openUri('mongodb://127.0.0.1:27017/app_db', { /* options */ });
Mongoose #save() 猫鼬#save()
var article = new Article(Obj);
article.save().then(function(result) {
return res.status(201).json({
message: 'Saved message',
obj: result
});
}, function (err) {
if (err) {
return res.status(500).json({
title: 'Ac error occurred',
error: err
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.