猫鼬模式中的“转换”如何工作?

时间:2020-12-27 07:17:16

标签: javascript node.js mongodb mongoose

我的用户模型图片:enter image description here 在我的用户模型中,我将此作为用户模型的第二个参数,以删除 __v 并将 _id 替换为 id

  {
    toJSON: {
      transform: function (doc, ret) {
        ret.id = ret._id;
        delete ret._id;
        delete ret.password;
        delete ret.__v;
      },
    },
  }

在我的登录路由器中,我有这样的东西:

const existingUser = await User.findOne({email});
console.log("existingUser*", existingUser)
res.status(200).send(existingUser);

我从我的 console.log 得到这个

 {
  _id: 5fe81e29fdd22a00546b05e3,
  email: 'chs@hotmail.fr',
  password: '0636b425ef0add0056ec85a5596eacf9ff0c71f8c2a1d4bad068a8679398e11870df12262722b911502eacb5fca23cef0cdd3b740481102ead50c58756d14a34.3f82d856ad93bc99',
  __v: 0
}

但是在邮递员中我收到了这个:

{
    "email": "chs@hotmail.fr",
    "id": "5fe81e29fdd22a00546b05e3"
}

我知道使用转换,“如果设置,猫鼬将调用此函数以允许您转换返回的对象”。
但是有人可以向我解释什么时候发生“转换”以证明 console.log 和我在邮递员中收到的数据之间的差异是合理的?
这和异步有关系吗?

0 个答案:

没有答案