承载令牌为什么会给出错误“无法读取未定义的属性'split'?”

问题描述 投票:0回答:1

我在这里要做的所有事情都是为了保护快速通道。从反应发送请求到快递路由器:

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'

它与邮递员工作正常,但是当我从响应发送请求时,它不断重复此错误,我被卡在这里,请帮助!

node.js reactjs express axios bearer-token
1个回答
0
投票

[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()
© www.soinside.com 2019 - 2024. All rights reserved.