我有一个 SequelizeInstance 对象,当我将其更改为 JSON 时,我需要省略一些值,但每当我调用 delete 时,它也会从 SequelizeInstance 中删除该值。下面是代码:
instanceMethods: {
toJSON: function() {
var values = this.get();
delete values.password;
delete values.salt;
return values;
}
}
我面临的问题是,每当我调用 toJSON() 时,SequelizeInstance 都会删除密码和盐的值。我可以做的一个可能的解决方案是对对象进行浅拷贝并返回它。我想知道是否还有其他更好的方法来解决这个问题。
最佳答案
直接来自 Sequelize issue
password: {
// ...
get() {
return () => this.getDataValue('password');
}
}
作者引言
Returning a function and not a value from user.password. The pros part of it is the explicit need for user.password() call to retrieve the pass. The pass will be automatically evaded by ordinary serialization too
关于json - NodeJs Sequelize 删除 toJSON 方法中的属性会导致从模型本身中删除值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39282062/