accessToken。路由器
router.get("/my-orders", auth.verify, (req, res) => {
const user = auth.decode(req.headers.authorization);
if (!user.isAdmin) {
UserController.getMyOrders(req.body).then(getMine => res.send(getMine));
} else {
return res.status(403).send("Access denied.");
}
});```控制器
module.exports.getMyOrders = (body) => {
return User.find({}, {
"isAdmin": 0,
"_id": 0,
"password": 0
});
}我什么都得到了。有人能帮我写代码吗?如何过滤令牌所属的用户,检索他的订单,却无法获得其他用户的订单?
发布于 2021-06-09 17:11:52
通过在.find方法中传递一个空对象,可以告诉mongodb查找所有内容。我假设在body中您有一些数据来查找特定的用户,如果是这样的话,您将使用它。例如:如果body包含一个用户名,你可以写.
module.exports.getMyOrders = (body) => {
return User.find({username: body.username});
}下面是关于db.collection.find()的更多信息
编辑-通过JWT查找用户:
router.get("/my-orders", auth.verify, (req, res) => {
//Here you have decoded your JWT and saved it as user
const user = auth.decode(req.headers.authorization);
if (!user.isAdmin) {
//here you are passing user instead of req.body
UserController.getMyOrders(user).then(getMine => res.send(getMine));
} else {
return res.status(403).send("Access denied.");
}
});module.exports.getMyOrders = (user) => {
//now you are using 'username' from the decoded jwt to look up the user
return User.find({username: user.username});
}https://stackoverflow.com/questions/67908506
复制相似问题