제 삭제 경로에서 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: [] }]
})
은 왜 이런 일에 몇 가지 통찰력을 감사하겠습니다. 감사!