Mongoose 中间件 post 更新不工作
Mongoose middleware post update not working
回调没有被调用,但它应该按照 mongoose 中间件中的记录:
schema.post('update', function(error, res, next) {
if (error.name === 'MongoError' && error.code === 11000) {
next(new Error('There was a duplicate key error'));
} else {
next(error);
}
});
我试过预更新并且有效:
schema.pre("update", function(next) {
console.warn('results', "i am called");
next(new Error("error line called"));
});
但我想要的是post更新:
schema.post("update", function(error, res, next) {
console.warn('results', "is this called?");
});
实际模型更新:
MyModel.update({_id : 123}, req.payload, function (err, numberAffected, rawResponse) {
reply("done!");
});
我没有看到日志 console.warn('results', "is this called?");
,这是预期的吗?
p.s:
机器:windows10,
猫鼬版本:4.5.8
根据 the docs,您似乎应该在 schema.post
的回调函数中只有一个参数来表示已更新的文档。您的回调很可能永远不会被挂钩调用,因为它从未提供其余参数。例如:
schema.post("update", function(doc) {
console.log('Update finished.');
});
而不是:
schema.post("update", function(error, res, next) {
console.log('Update finished.');
});
回调没有被调用,但它应该按照 mongoose 中间件中的记录:
schema.post('update', function(error, res, next) {
if (error.name === 'MongoError' && error.code === 11000) {
next(new Error('There was a duplicate key error'));
} else {
next(error);
}
});
我试过预更新并且有效:
schema.pre("update", function(next) {
console.warn('results', "i am called");
next(new Error("error line called"));
});
但我想要的是post更新:
schema.post("update", function(error, res, next) {
console.warn('results', "is this called?");
});
实际模型更新:
MyModel.update({_id : 123}, req.payload, function (err, numberAffected, rawResponse) {
reply("done!");
});
我没有看到日志 console.warn('results', "is this called?");
,这是预期的吗?
p.s: 机器:windows10, 猫鼬版本:4.5.8
根据 the docs,您似乎应该在 schema.post
的回调函数中只有一个参数来表示已更新的文档。您的回调很可能永远不会被挂钩调用,因为它从未提供其余参数。例如:
schema.post("update", function(doc) {
console.log('Update finished.');
});
而不是:
schema.post("update", function(error, res, next) {
console.log('Update finished.');
});