如何解决当我访问这条路线时,它没有在 url 中提供用户 ID
router.get("/dashboard/profile/:userid",auth,async (req,res) => {
try {
const userid = req.params.user.id;
// request.user is getting fetched from Middleware after token authentication
const user = await User.findById(userid);
res.json(user);
} catch (e) {
res.send({ message: "Error in Fetching user" });
}
});
module.exports = router
这是我的获取路径,我试图从用户那里获取 params.id 但它不起作用
解决方法
替换这个
const userid = req.params.user.id
与
const userid = req.params.userid
,
userid
是单个键。但是你用 user.id
分开了。所以使用
const userid = req.params.userid;
代替
const userid = req.params.user.id;
仅获取 localhost:3000/dashboard/profile 不起作用?
您的路径中缺少 userid
值。
请检查是否有三段。
dashboard/profile/:userid
1 2 3
但你的道路是(问题)
localhost:3000/dashboard/profile
1 2
请不要考虑主机名和端口。在 /
解决方案
localhost:3000/dashboard/profile/YourUserID
1 2 3(//add your userid here)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。