TypeError: Cannot read property 'split'在Nodejs中未定义
TypeError: Cannot read property 'split' of undefined in Nodejs
使用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'
});
}
...
};
Edit:但是为什么你想要第二个令牌而不是第一个呢?
相关文章:
- 在gump和nodejs中使用Typescript时,未定义对require和exports的引用
- Nodejs模块函数返回未定义的,而不是异步的
- 无法读取未定义的属性 - NodeJS,Express,Mongoose
- Nodejs会话未定义
- NodeJS'到UpperCase'的未定义(从typescript生成)
- 类型错误:无法读取 Nodejs 中未定义的属性“post”
- 使用 NodeJS 的请求模块进行 HTTP 身份验证,返回未定义的正文
- NodeJS 错误堆栈未定义
- 在 NodeJS 中未定义为 falsey
- nodejs socket 发出返回未定义
- Nodejs - Req.body 未定义在 post 中,带有快速 4.9.0
- Javascript 函数在 nodejs 中返回未定义的值
- NodeJS 错误与 connect-busboy,“类型错误:无法调用未定义的方法'on'”
- 对象未定义到模块中,Nodejs javascript
- 无法读取 nodejs 中未定义的属性“then”
- 类型错误:无法读取 nodejs 应用程序中未定义的属性“方法”
- Nodejs JSON 属性未定义
- 未定义的 NodeJS 爬网程序不是一个函数
- nodejs-mocha套件未定义错误
- Nodejs:session生成后未定义