未删除事件是否为猫鼬模式触发? [英] Pre remove event not firing for mongoose schema?

查看:52
本文介绍了未删除事件是否为猫鼬模式触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这没有开火!不知道为什么,非常感谢您的帮助.

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();
 });

推荐答案

根据 doc

对于remove()没有查询挂钩,仅针对文档.如果设置了'remove'挂钩,则在调用myDoc.remove()时将触发该钩子,而不是在调用MyModel.remove()时将其触发.

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().

那么您可以使用这个

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.
           });
       })
    }
});

您可以从讨论

这篇关于未删除事件是否为猫鼬模式触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆