一尘不染

(node:3341)弃用警告:猫鼬:mpromise

node.js

我正在尝试使用自定义方法在猫鼬的顶部开发一个类,因此我用自己的类扩展了猫鼬,但是当我调用创建一个新的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;
 }
}

关于我在做什么错的任何想法?


阅读 185

收藏
2020-07-07

共1个答案

一尘不染

阅读文档后,这对我来说是解决问题的方法:http :
//mongoosejs.com/docs/promises.html

该文档中的示例使用的是bluebird Promise库,但我选择使用本机ES6 Promise。

在我要呼叫的档案中mongoose.connect

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

[EDIT: Thanks to @SylonZero for bringing up a performance flaw in my answer.
Since this answer is so greatly viewed, I feel a sense of duty to make this
edit and to encourage the use of bluebird instead of native promises. Please
read the answer below this one for more educated and experienced details. ]

2020-07-07