【问题标题】:populated objects doesn't seem to show up in postman's response body填充的对象似乎没有出现在邮递员的响应正文中
【发布时间】:2021-04-04 08:23:57
【问题描述】:

我已经用用户名和头像填充了查询结果

  router.get("/me", auth, async (req, res) => {
      try {
        const profile = await Profile.findOne({
          user: req.user.id,
        }).populate("user", ["name", "avatar"]);

    if (!profile) {
      return res.status(400).json({ msg: "there is no profile for this user" });
    }
    res.json({ profile });
    console.log("profile", profile);
  } catch (err) {
    console.error(err);
    res.status(500).send("Server error");
  }
});

当我在 Postman 应用程序中向这个 url 发出获取请求时,我在 Postman 中得到的响应的用户属性是这样的:

"user":"a string",

但是当我在控制台内部检查时是这样的:

 user: {
        id:"a string",
        name:"username",
        avatar:"avatarUrl"
       }

这背后的原因是什么?

【问题讨论】:

    标签: reactjs mongoose postman


    【解决方案1】:

    我对这段代码没有任何问题:https://codesandbox.io/s/node-js-forked-j340i?file=/src/index.js 也许与邮递员有关?您可以尝试重新安装它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-14
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多