Reputation: 1678
I'm using mongoose and trying to cascade a delete but unfortunately my pre remove middle ware is not firing for some reason.
var presentationSchema = new Schema({
id: Number,
title: String,
pdfURL: String,
created_at: Date,
updated_at: Date,
slides: [{
type: Schema.Types.ObjectId,
ref: 'Slide'
}]
});
presentationSchema.pre('remove', function(next) {
console.log("delete slides" + this._id);
next();
});
// the schema is useless so far
// we need to create a model using it
var Presentation = mongoose.model('Presentation', presentationSchema);
// make this available to our users in our Node applications
module.exports = Presentation;
Upvotes: 2
Views: 1097
Reputation: 45402
Maybe you get hooked by this mongoose "feature" :
Note: There is no query hook for remove(), only for documents. If you set a 'remove' hook, it will be fired when you call myDoc.remove(), not when you call MyModel.remove(). Note: The create() function fires save() hooks.
Your pre('remove',...)
middleware will be fired when you call myPres.remove()
not when calling remove function from the model like Presentation.remove()
Upvotes: 4