我很確定我將實體方法定義為一個函式,但是TypeError: foundProduct.toggleOnSale is not a function在呼叫標有<--. 我不明白為什么會這樣。
const Product = mongoose.model('Product', productSchema)
productSchema.methods.toggleOnSale = function() {
this.onSale = !this.onSale;
return this.save();
}
const findProduct = async () => {
const foundProduct = await Product.findOne({ name: 'Mountain Bike' });
console.log(foundProduct)
await foundProduct.toggleOnSale() <--
console.log(foundProduct)
}
uj5u.com熱心網友回復:
我認為您可能想在從中派生模型類之前嘗試在模式上定義方法:
const productSchema = /* ... */;
productSchema.methods.toggleOnSale = function() {
this.onSale = !this.onSale;
return this.save();
}
const Product = mongoose.model('Product', productSchema)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/449270.html
