问题
This isn't firing! No idea why, would really appreciate help here.
Booking.find({_id: key}).remove(function(err, result){
if (err) { console.err("ERR", err) }
else {
console.log("remove result", result);
}
})
BookingSchema.pre('remove', function (next) {
console.log("THIS ID", this._id);
next();
});
回答1:
Per the doc,
There is no query hook for
remove()
, only for documents. If you set a'remove'
hook, it will be fired when you callmyDoc.remove()
, not when you callMyModel.remove()
.
Then you can use this one
Booking.find({_id: key}, function(err, books){
if (err)
throw err;
else {
books.forEach(function(book){
book.remove(function(err){
// the 'remove' pre events are emitted before this book is removed.
});
})
}
});
You can get more information from this discussion
来源:https://stackoverflow.com/questions/36191228/pre-remove-event-not-firing-for-mongoose-schema