猫鼬中间件预更新 [英] mongoose middleware pre update
问题描述
我正在使用
schema.pre('save', function (next) {
if (this.isModified('field')) {
//do something
}
});
但是我现在需要在schema.pre('update'
挂钩中使用相同的功能isModified
,但是它不存在.有谁知道我如何在update
挂钩中使用相同的功能?
but I now need to use this same function isModified
in a schema.pre('update'
hook, but it does not exists. Does anyone know how I can use this same functionality in the update
hook?
推荐答案
根据此:
查询中间件与文档中间件的区别很细微,但 重要方式:在文档中间件中,这是指文档 正在更新.在查询中间件中,猫鼬不一定具有 对要更新的文档的引用,因此这是指查询 对象而不是要更新的文档.
Query middleware differs from document middleware in a subtle but important way: in document middleware, this refers to the document being updated. In query middleware, mongoose doesn't necessarily have a reference to the document being updated, so this refers to the query object rather than the document being updated.
update
是查询中间件,this
引用没有isModified
方法的查询对象.
update
is query middleware and this
refers to a query object which has no isModified
method.
这篇关于猫鼬中间件预更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!