如何正确地从日志方法中删除Mongoose模型字段



使用Mongoose处理模式和模型时,我得到了一个password字段,当为用户提供API时,必须删除该字段。

我需要做一些类似的事情:

var user = JSON.parse(JSON.stringify(mongooseUserModel));
delete user.password;
// return ....

因此,对于console.logJSON.stringify等形式的任何函数,我都需要使该过程隐含。

我并没有试图从查询中排除密码字段,只是我不希望它被记录下来。

功能转换应该是您想要的。

以下是您需要的示例:

UserSchema.set('toJSON', {
  transform: function(doc, ret, options) {
    delete ret.password;  // just delete password field when toJSON
  }
});

最新更新