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

编程入门 行业动态 更新时间:2024-10-06 22:33:18

承载<a href=https://www.elefans.com/category/jswz/34/1771317.html style=令牌为什么会给出错误“无法读取未定义的属性'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'?”

本文发布于:2024-05-07 04:54:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754334.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:令牌   会给   属性   错误   未定义

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!