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

时间:2015-11-20 21:21:27

标签: node.js mongodb mongoose

使用Mongoose的工作模式和模型我有一个password字段,在向API提供用户时我必须删除它。

我需要做类似的事情:

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

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

我不是要从查询中排除密码字段,只是我不希望它被记录。

1 个答案:

答案 0 :(得分:0)

功能transform应该正是您想要的。

以下是您需要的示例:

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