node.js - ( Node :3341) DeprecationWarning: Mongoo

我正在尝试使用我的自定义方法在 mongoose 上开发一个类,所以我用我自己的类扩展了 mongoose,但是当我调用创建一个新的 car 方法时它可以工作,但是它的 strip 和错误,在这里我让你看看我在做什么。

我收到此警告

(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;
 }
}

对我做错了什么有什么想法吗?

最佳答案

在阅读文档后,这对我来说是解决问题的方法: 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');

[编辑:感谢@SylonZero 在我的回答中提出了性能缺陷。由于这个答案的浏览量如此之大,我感到有责任进行此编辑并鼓励使用 bluebird 而不是原生 promise 。请阅读下面的答案以获取更多受过教育和经验丰富的详细信息。 ]

关于node.js - ( Node :3341) DeprecationWarning: Mongoose: mpromise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38138445/

相关文章:

node.js - 如何获得 Mongoose 模型的所有计数?

linux - 无法连接到服务器 127.0.0.1 shell/mongo.js

mongodb - 在 MongoDB 中查找重复记录

MongoDB GPG - 无效签名

mongodb - Mongoose 的 $or 条件的 find 方法无法正常工作

mongodb - GridFS 对于生产来说是否足够快速和可靠?

mongodb - 未指定排序顺序时,MongoDB 如何对记录进行排序?

mongodb - 如何在 MongoDB 中将集合导出到 CSV?

mongodb - 为什么 Mongoose 既有模式又有模型?

javascript - Mongoose .js : Find user by username