gpt4 book ai didi

node.js - 预删除中间件不触发

转载 作者:搜寻专家 更新时间:2023-11-01 00:38:40 24 4
gpt4 key购买 nike

我正在使用 mongoose 并尝试级联删除,但不幸的是我的预删除中间件由于某种原因没有触发。

  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;

最佳答案

也许您会被 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.

你的 pre('remove',...) 中间件将在你调用 myPres.remove() 时触发,而不是在从模型中调用 remove 函数时触发,例如 Presentation.remove()

关于node.js - 预删除中间件不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42489302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com