使用Mongoose处理模式和模型时,我得到了一个password
字段,当为用户提供API时,必须删除该字段。
我需要做一些类似的事情:
var user = JSON.parse(JSON.stringify(mongooseUserModel));
delete user.password;
// return ....
因此,对于console.log
、JSON.stringify
等形式的任何函数,我都需要使该过程隐含。
我并没有试图从查询中排除密码字段,只是我不希望它被记录下来。
功能转换应该是您想要的。
以下是您需要的示例:
UserSchema.set('toJSON', {
transform: function(doc, ret, options) {
delete ret.password; // just delete password field when toJSON
}
});