I have 2 Schemas CategorySchema and ProductSchema :
i have made a middleware in CategorySchema but it dosn't work
here is the CategorySchema :
const mongoose = require('mongoose');
const CategorySchema = new mongoose.Schema({
catName :{
type : String,
required : true,
},
intro : {
type : String,
required : true
},
img : {
type : String,
required : true
},
products : {
type : [{type : mongoose.Schema.Types.ObjectId, ref:"Product"}]
}
});
CategorySchema.pre("deleteOne",function(next){
const product = mongoose.model("Product");
product.deleteOne({_id : {$in : this.products}}).then(next());
})
module.exports = mongoose.model('Category',CategorySchema);
question from:
https://stackoverflow.com/questions/65937981/how-to-delete-mongoose-document-with-references 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…