微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决node:3341弃用警告:猫鼬:mpromise

阅读文档后,这对我来说是解决问题的方法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. ]

解决方法

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。