只有在创建新文档时才会调用Mongoose Setters? [英] Mongoose Setters only get called when create a new doc?

查看:98
本文介绍了只有在创建新文档时才会调用Mongoose Setters?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Mongoose getter和setter我最近发现setter仅在我创建新doc(UserSchema.create ...)时适用,但setter不会被调用更新(UserSchema.findByIdAndUpdate ...)。我是对的还是我错过了什么。如果我是对的,是否有更新的setter?

I use Mongoose getter and setter I recently found that setter only applies when I create a new doc (UserSchema.create...) but setter will not get called for update (UserSchema.findByIdAndUpdate...). Am I right or am I missing anything. If I'm right, is there setter for update?

http://mongoosejs.com/docs/2.7.x/docs/getters-setters.html

谢谢。

更新

我发现git问题: https://github.com/LearnBoost/mongoose/issues/751 ,这意味着这是预期的行为。不确定我是对的。

I found git issue: https://github.com/LearnBoost/mongoose/issues/751 , it means this is expected behavior. Not sure I'm right.

推荐答案

发现我不应该使用findByIdAndUpdate,而应该查询对象,编辑和保存。

Found out that I should not use findByIdAndUpdate, instead I should query object, edit and save.

这篇关于只有在创建新文档时才会调用Mongoose Setters?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆