最佳答案
我试图用我的自定义方法在猫鼬的顶部开发一个类所以我用我自己的类扩展了猫鼬但是当我调用创建一个新的 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 类的实例
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;
}
}
有没有想过我做错了什么?