令牌为什么会给出错误“无法读取未定义的属性'split'?”"/>
承载令牌为什么会给出错误“无法读取未定义的属性'split'?”
我在这里要做的所有事情都是为了保护快速通道。从反应发送请求到快递路由器:
Axios.post("http://localhost:5000/users/userinfo/" + this.state.id, {
headers: { Authorization: "Bearer " + "jsonwebtoken" },
data:{this.state.data},
})
.then((res) => console.log(res.data))
.catch((err) => console.log(err));
现在此请求命中快速路由器:
router.post("/userinfo/:id", verifyToken, (req, res) => {
res.json({ msg: "authorized", data: req.payback });
)}
这里是verfifyToken中间件:
module.exports = function (req, res, next) {
try {
const decode = req.headers.authorization.split(" ")[1];
req.token = decode;
jwt.verify(req.token, process.env.JWT_SECRET, (err, data) => {
if (!err) {
req.payback = data;
next();
} else {
return res.json({ error: "Unauthorized User!" });
}
});
} catch (error) {
console.log(error);
res.json({ error: error });
}
};
但是这给了我错误:TypeError:无法读取未定义的属性'split'
它与邮递员工作正常,但是当我从响应发送请求时,它不断重复此错误,我被卡在这里,请帮助!
回答如下:[axios.post
具有以下签名axios.post
因此,您必须执行以下操作。
axios#post(url[, data[, config]])
此外,如果只需要一个标头字段,则最好使用Axios.post("http://localhost:5000/users/userinfo/" + this.state.id,
this.state.data, {
headers: { Authorization: "Bearer " + "jsonwebtoken" },
})
或别名req.get()
以避免大小写不匹配
req.get()
更多推荐
承载令牌为什么会给出错误“无法读取未定义的属性'split'?”
发布评论