TypeError: Cannot read property 'token' of undefined

TypeError: Cannot read property 'token' of undefined

我遇到了这个错误:TypeError: Cannot read 属性 'token' of undefined - 这里是 a link!到我的 github 页面

我正在使用 firefox 插件来 post 数据并以 json 格式发送数据,例如:{"x-access-token": "jashiuahf"}

这是图片enter image description here @fabio

谁能帮我解决这个问题!!!

看起来错误来自这些行

app.use(function(req, res, next) {
console.log(req.headers['x-access-token']);
var token = req.body.token || req.body.query || req.headers['x-access-token'];
........ more code ....

我认为 req.body.tokenreq.body.query 不是寻找身份验证或访问令牌的地方,总是尝试在请求 header 中寻找它。错误是因为 token 不是 req.body 的 属性,只是

var token = req.headers['x-access-token'] || '';

应该可以。

同时检查这个问题:get authorization header token with node js

我在使用 Node 和 Express 时也遇到了同样的错误..

原来我不需要cookie-parser顶部的依赖确保你需要并安装它

const cookieParser = require('cookie-parser');
app.use(cookieParser());