本文介绍了猫鼬是否提供对pre('save')中属性的先前值的访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在pre('save')
中间件中将属性的新/传入值与该属性的先前值(当前在db中保存的值)进行比较.
I'd like to compare the new/incoming value of a property with the previous value of that property (what is currently saved in the db) within a pre('save')
middleware.
猫鼬是否提供执行此操作的功能?
Does Mongoose provide a facility for doing this?
推荐答案
猫鼬允许您配置自定义设置器来进行比较. pre('save')本身不会给您所需的东西,而是在一起:
Mongoose allows you to configure custom setters in which you do the comparison. pre('save') by itself won't give you what you need, but together:
schema.path('name').set(function (newVal) {
var originalVal = this.name;
if (someThing) {
this._customState = true;
}
});
schema.pre('save', function (next) {
if (this._customState) {
...
}
next();
})
这篇关于猫鼬是否提供对pre('save')中属性的先前值的访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!