javascript - 类型错误 : Cannot read property 'split' of undefined in Nodejs

标签 javascript node.js jwt

我收到以下错误,使用 jwt-simple 库:

TypeError: Cannot read property 'split' of undefined
    at module.exports (C:\my_application\services\mylist.js:5:40)
    at Layer.handle [as handle_request] (C:\my_application\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\my_application\node_modules\express\lib\router\route.js:131:13)
    at Route.dispatch (C:\my_application\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\my_application\node_modules\express\lib\router\layer.js:95:5)
    at C:\my_application\node_modules\express\lib\router\index.js:277:22
    at Function.process_params (C:\my_application\node_modules\express\lib\router\index.js:330:12)
    at next (C:\my_application\node_modules\express\lib\router\index.js:271:10)
    at C:\my_application\api.js:39:3
    at Layer.handle [as handle_request] (C:\my_application\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\my_application\node_modules\express\lib\router\index.js:312:13)
    at C:\my_application\node_modules\express\lib\router\index.js:280:7
    at Function.process_params (C:\my_application\node_modules\express\lib\router\index.js:330:12)
    at next (C:\my_application\node_modules\express\lib\router\index.js:271:10)
    at logger (C:\my_application\node_modules\morgan\index.js:144:5)
    at Layer.handle [as handle_request] (C:\my_application\node_modules\express\lib\router\layer.js:95:5)

这是 mylist.js 文件:

var jwt = require('jwt-simple');


module.exports = function (req, res) {
  var token = req.headers.authorization.split(' ')[1];
  var payload = jwt.decode(token, "shhh..");
  if(!payload.sub) {
    res.status(401).send({
      message: 'Authentication failed'
    });
  }
  if(!req.headers.authorization){
    return res.status(401).send({
      message: 'You are not authorized'
    });
  }
  res.json(mylist);
};

var mylist = [
  'Proj 1',
  'Proj 2',
  'Proj 3',
  'Proj 4'
];

我正在尝试查看用户是否有权访问前端的 mylist 资源。
有人知道吗?

最佳答案

你假设它是一个字符串,即使你不知道那里是否真的有一个字符串。 你应该先添加一些错误检查

module.exports = function (req, res) {
  if (typeof req.headers.authorization !== 'string') {
    res.sendStatus(400);
    return;
  }

  var tokens = req.headers.authorization.split(' ');

  if (tokens.length < 2) {
    res.sendStatus(400);
    return;
  }

  var token = tokens[1];

  var payload = jwt.decode(token, "shhh..");
  if(!payload.sub) {
    res.status(401).send({
      message: 'Authentication failed'
    });
  }
  ...
};

编辑:但是为什么您想要第二个 token 而不是第一个呢?

关于javascript - 类型错误 : Cannot read property 'split' of undefined in Nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39102998/

相关文章:

javascript - 为什么 jQuery 选择器变量不能在命名空间内工作?

javascript - 使用 window.print content 将网页下载为 pdf

javascript - 在 Electron 中使用 AWS Cognito 的持久用户 session

node.js - Docker 容器中的 NodeJS 14 无法连接到 Postgres DB(进/出 docker)

javascript - 部署到 Vercel 时,Sendgrid 电子邮件不会发送

node.js - 验证错误 : "expiresInMinutes" is not allowed NodeJs JsonWebToken

javascript - 我应该在哪里存储 JWT token ?

c# - 使用 Net Core 2 的 JWT 远程身份验证错误

javascript - Golden Layout + Angular2 RC5 - 无法使用 ViewContainerRef 动态创建组件

javascript - JS : Touch equivalent for mouseenter