'TypeError: Cannot read property 'token' of undefined
I am facing this error : TypeError: Cannot read property 'token' of undefined - Here's a link! to my github page
I am using a firefox addon to post data and sending data in json format like: {"x-access-token": "jashiuahf"}
Here is the image enter image description here @fabio
Can anybody help me solve this please!!!
Solution 1:[1]
It looks like the error comes from these lines
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 ....
I think that req.body.token or req.body.query is not the place to look for authentication or access token, always try to look for it in the request header. The error is because token is not a property of req.body, just
var token = req.headers['x-access-token'] || '';
should work.
also check this question: get authorization header token with node js
Solution 2:[2]
I also faced the same error while working with Node and Express..
Turns out I didn't require cookie-parser dependency at the top make sure you require and mount it
const cookieParser = require('cookie-parser');
app.use(cookieParser());
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Fabio Carballo |
| Solution 2 | Pratham |
