【发布时间】: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"
}
这背后的原因是什么?
【问题讨论】: