Not able to delete cascade as Model.remove() fuction is not available in mongoose.
using findByIdAndDelete is not invoking the middleware:
BootcampSchema.pre('remove',async function(next){ //before deleting the doc
console.log(`courses being removed from bootcamp ${this._id}`);
await this.model('Course').deleteMany({bootcamp:this._id});
next();
});
tried different delete methods but can’t delete cascade
2
Answers
Try using
findOneAndRemove
:This code is from Bootcamp model, I accidentally named it with C capital in Camp so if you named it Bootcamp just replace it in with your naming convention.
This code is from bootcamp controler
This works perfectly for me, so I hope it’ll work for others as well.