Node.js &引用;前";及;邮政「;删除未启动的中间件

Node.js &引用;前";及;邮政「;删除未启动的中间件,node.js,mongodb,mongoose,middleware,Node.js,Mongodb,Mongoose,Middleware,我已经实现了两种不同的方法来删除用户,但没有一种方法触发“pre”和“post”删除中间件 User.remove(callback)//Query.remove() 据我所知 下面是我在模型文件中的两个不同实现: 方法一: var User = module.exports = mongoose.model('User', userSchema); userSchema.pre('remove', function(next) { // 'this' is the client b

我已经实现了两种不同的方法来删除用户,但没有一种方法触发“pre”和“post”删除中间件

User.remove(callback)//Query.remove()
据我所知

下面是我在模型文件中的两个不同实现:

方法一:

var User = module.exports = mongoose.model('User', userSchema);

userSchema.pre('remove', function(next) {
    // 'this' is the client being removed. Provide callbacks here if you want
    // to be notified of the calls' result.
    //Vouchers.remove({user_id: this._id}).exec();
    console.log("pre test");
    next();
});

userSchema.post('remove', function(next) {
    // 'this' is the client being removed. Provide callbacks here if you want
    // to be notified of the calls' result.
    //Vouchers.remove({user_id: this._id}).exec();
    console.log("post test");
    next();
});

// Remove User
module.exports.removeUser = function(id, callback){
    var query = {_id: id};
    console.log('test');
    //User.remove(query, callback);
    User.find(query).remove(callback);

}
方法二:

var User = module.exports = mongoose.model('User', userSchema);

userSchema.pre('remove', function(next) {
    // 'this' is the client being removed. Provide callbacks here if you want
    // to be notified of the calls' result.
    //Vouchers.remove({user_id: this._id}).exec();
    console.log("pre test");
    next();
});

userSchema.post('remove', function(next) {
    // 'this' is the client being removed. Provide callbacks here if you want
    // to be notified of the calls' result.
    //Vouchers.remove({user_id: this._id}).exec();
    console.log("post test");
    next();
});

// Remove User
module.exports.removeUser = function(id, callback){
    var query = {_id: id};
    console.log('test');
    User.remove(query, callback);
}

请参阅文档。根据设计,移除的中间件钩子不是针对Model.remove启动的,而是针对ModelDocument.remove函数启动的

我就是这样让一切运转起来的:

// Remove User
module.exports.removeUser = function(id, callback){

    User.findById(id, function (err, doc) {
        if (err) {

        }

        doc.remove(callback);
    })
}

//Remove vouchers related to users
userSchema.pre('remove', function(next) {
    this.model('Voucher').remove({ user: this._id }, next);
});

其他人也在搜索相同的内容。deleteOne和deleteMany中间件可用于前置和后置中间件。已从最新的mongo版本中弃用Remove

下面的代码对我有用

   ModelSchema.pre("deleteOne", { document: true }, function(next) {
      let id = this.getQuery()["_id"];
      mongoose.model("Model_two").deleteMany({ property: id }, function(err, result) {
        if (err) {
          next(err);
        } else {
          next();
        }
      });
    });

对于将来出现此问题的任何人,请在调用
mongoose.model()
之前检查是否定义了钩子

在编译模型之前定义中间件 在Mongoose中调用
pre()
post()
after通常不起作用。例如,下面的
pre('save')
中间件将不会启动

您需要添加

{document:false,query:true}
userSchema('remove',callback)
Query.remove()的中间件的参数。理解查询中间件和文档中间件之间的区别也很重要

User.remove(callback)//Query.remove()
查询中间件和Query.remove()
UserSchema.pre('remove',callback)
如果是查询中间件,则默认情况下不会激发它。作为解决办法

var User = module.exports = mongoose.model('User', userSchema);
//...
userSchema.pre('remove',{document:false,query:true}, callback);
//...
module.exports.removeUser = function(id, callback){
    User.remove(callback);
}

我们将
{document:false,query:true}
参数传递给中间件

User.remove(callback)//Query.remove()
不会从数据库返回文档。它将执行删除操作,并且不会调用
doc.remove()
。它将调用
Query.remove()

文档中间件和doc.remove()
userSchema.pre('remove',callback)
默认情况下注册为文档中间件

下面的代码片段将在没有其他参数的情况下平稳运行

var User = module.exports = mongoose.model('User', userSchema);
//...
userSchema.pre('remove', callback);
//...
module.exports.removeUser = function(id, callback){
    User.find(query).remove(callback);//doc.remove()
}


userSchema.pre('remove',callback)
将被触发,因为
User.find(query)
从数据库返回mongoose文档,而
User.find(query)
remove(callback)
将调用
doc.remove()
仅调用文档中间件

// query document and delete
schema.findOne({ _id: id}).then((doc) => {
  doc.remove().then(() => {
    console.log(`it works!`)
  })
})
// schema
schema.pre('remove', { document: true, query: false }, function() {
  console.log('Removing doc!')
})
删除已弃用,请尝试改用deleteOne

只有查询中间件。当您执行
Model.deleteOne()
而不是
doc.remove()
时,将调用此函数


这和我的回答有什么不同?你的回答根本帮不了我。我还可以让人们查阅文档,让他们自己去弄清楚。当我回答问题时,我试图详细说明和解释什么和为什么,因此我相应地投了赞成票。我只是发现任何人都可以浏览文档并推荐他人。@SiegfriedGrimbeek,尽管您没有给出描述如何制作
Model.remove()
的答案,也没有明确说明在这种情况下钩子不会运行。如果你能解释为什么Mongoose只能在实例上运行钩子,那会很有帮助。神圣文档:
Model.remove()-此方法直接向MongoDB发送remove命令,不涉及Mongoose文档。因为不涉及Mongoose文档,所以不会执行中间件(钩子)。
在上面的示例中,首先找到文档,然后将其删除。注意:remove()没有查询钩子,只针对文档。如果设置了“remove”钩子,则在调用myDoc.remove()时会触发该钩子,而不是在调用MyModel.remove()时触发该钩子。如果这是出于设计,如何更改设计?已保存我的一天,谢谢;)