我正在尝试使用我的自定义方法在 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/