Pre remove event not firing for mongoose schema?

好久不见. 提交于 2019-12-24 16:52:45

问题


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 call myDoc.remove(), not when you call MyModel.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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!