首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >(node:3341) DeprecationWarning: Mongoose: mpromise

(node:3341) DeprecationWarning: Mongoose: mpromise
EN

Stack Overflow用户
提问于 2016-07-01 14:25:28
回答 5查看 57.6K关注 0票数 90

我试图用我的自定义方法在mongoose的顶部开发一个类,所以我用我自己的类扩展了mongoose,但是当我调用创建一个新的car方法时,它可以工作,但它的条带和错误,这里我让你看到我想要做的事情。

我收到了这个警告

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

在我做完之后

driver.createCar({
      carName: 'jeep',
      availableSeats: 4,
    }, callback);

driver是Driver类的实例

const carSchema = new Schema({
  carName: String,
  availableSeats: Number,
  createdOn: { type: Date, default: Date.now },
});
const driverSchema = new Schema({
 email: String,
 name: String,
 city: String,
 phoneNumber: String,
 cars: [carSchema],
 userId: {
   type: Schema.Types.ObjectId,
   required: true,
 },
createdOn: { type: Date, default: Date.now },
});
const DriverModel = mongoose.model('Driver', driverSchema);

class Driver extends DriverModel {
  getCurrentDate() {
  return moment().format();
}
create(cb) {
  // save driver
  this.createdOn = this.getCurrentDate();
  this.save(cb);
}
remove(cb) {
  super.remove({
  _id: this._id,
 }, cb);
}
createCar(carData, cb) {
  this.cars.push(carData);
  this.save(cb);
}
getCars() {
  return this.cars;
 }
}

有没有想过我做错了什么?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2016-07-02 04:50:35

在阅读了文档后,以下是我解决这个问题的有效方法:http://mongoosejs.com/docs/promises.html

文档中的示例使用的是bluebird promise库,但我选择使用原生ES6 promises。

在我调用mongoose.connect的文件中

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

编辑:感谢@SylonZero在我的回答中提出了一个性能缺陷。由于这个答案被广泛关注,我觉得有责任对此进行编辑,并鼓励使用bluebird而不是原生承诺。请阅读下面的答案,了解更多受过教育和经验丰富的细节。

票数 241
EN

Stack Overflow用户

发布于 2017-04-17 21:33:18

你试过这个吗?例如:

const mongoose = require('mongoose')
mongoose.Promise = global.Promise // <--
const Schema = mongoose.Schema
const UserSchema = new Schema({
  name: String,
})
const User = mongoose.model('user', UserSchema)
module.exports = User

如果您从mongoose实例创建模型,而mongoose实例的promise未重新定义-则此模型上的每个查询都会抛出警告。

票数 2
EN

Stack Overflow用户

发布于 2018-02-20 03:24:45

var mydb;
var uri = 'mongodb://localhost/user1';
var promise = mongooose.connect(uri,{
      useMongoClient: true,
});
promise.openUri(uri,function(errr,db){
if(errr){
        throw errr;
      }else{
        console.log("Connection Successfull");      
        mydb = db;
      }
});

在最新版本的mongoose中,你需要有promise的帮助,这是链接1:http://mongoosejs.com/docs/promises.html

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38138445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档