私の削除ルートではProduct.findByIdAndRemove(id)
を呼び出してproductScheme.pre('remove')
フックをトリガーします。 console.log
が実際に私の端末に何も出力しないことを除いて、私のコードは正常に動作します。製品ドキュメントがCategories.products配列から削除されているため、正常に機能しています。モンゴース事前削除は動作しますがConsole.logを出力しません
私の製品スキーマ
const productSchema = new Schema({
brandName: { type: String, required : true }, // e.g. Holden
name: { type: String, required : true }, // e.g. Commodore
categories: [{ type: Schema.ObjectId, ref: 'Category', default: [] }]
})
productSchema.pre('remove', next => {
console.log('PRODUCT PRE REMOVE') // <--- Never gets outputted. Why?
// The update works
Category.update(
{ products : { $in : this._id } },
{ $pull: { products: this._id } },
{ multi: true })
.exec()
next()
})
マイカテゴリースキーマ
const categorySchema = new Schema({
name: { type: String, unique: true, required : true }, // e.g. Cars
products: [{ type: Schema.ObjectId, ref: 'Product', default: [] }]
})
これが起こっている理由について、いくつかの洞察力をお願い申し上げます。ありがとう!