gpt4 book ai didi

javascript - TypeError : this. isModified 不是函数 - Mongoose 模型 FindOneAndUpdate

转载 作者:行者123 更新时间:2023-11-29 19:03:43 26 4
gpt4 key购买 nike

所以我想在地址更改时 Hook ,这样我就可以提醒用户更新。

但是,由于我的应用程序逻辑,更新对象始终是这样的:

Location.findByIdAndUpdate( options.location_id, { name: options.name, address: options.address }).exec()

现在我可以通过首先查找位置来重写它,在此之前首先检查 options.address 是否不同,如果相同则将其从更新中排除。

然后我可以使用

LocationSchema.pre('findOneAndUpdate', function(next) {
if (typeof this._update.address == undefined) {
return next();
}

console.log("address updated, time to notify");
next();
});

但我想知道我是否可以做一些类似于预保存方法中使用的常见逻辑的事情,就像这样:

LocationSchema.pre('findOneAndUpdate', function(next) {
if (!this.isModified('address')) {
return next();
}

console.log("address updated, time to notify");
next();
});

但不幸的是,TypeError: this.isModified 不是一个函数。

在模型逻辑内部执行此操作会更容易并节省调用。

最佳答案

我遇到了同样的问题。我解决了这个问题。

 userSchema.pre('findOneAndUpdate', function(next) {
// if password is not updated
if (!this._update.password) {
return next()
}

bcrypt.hash(this._update.password, 8, (err, hash) => {
if (err) {
return next(err)
}
this._update.password = hash
next()
})
})

关于javascript - TypeError : this. isModified 不是函数 - Mongoose 模型 FindOneAndUpdate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44613790/

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