techiediaries / fake-api-jwt-json-server

A Fake API with JWT Authentication using json-server and jsonwebtoken

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error on login: TypeError: Cannot destructure property `email` of "undefined" or "null"

jcornide opened this issue · comments

Hi,

I followed the steps to set up the JWT. Previously I was able to publish the API with the example data, but when it comes to the authentication I came across this error:

curl -d "email=bruno&password=bruno" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/auth/login
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>TypeError: Cannot destructure property `email` of &#39;undefined&#39; or &#39;null&#39;.<br> &nbsp; &nbsp;at server.post (/Users/jose/Documents/www/fake-api/server.js:35:33)<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (/Users/jose/Documents/www/fake-api/node_modules/express/lib/router/layer.js:95:5)<br> &nbsp; &nbsp;at next (/Users/jose/Documents/www/fake-api/node_modules/express/lib/router/route.js:137:13)<br> &nbsp; &nbsp;at Route.dispatch (/Users/jose/Documents/www/fake-api/node_modules/express/lib/router/route.js:112:3)<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (/Users/jose/Documents/www/fake-api/node_modules/express/lib/router/layer.js:95:5)<br> &nbsp; &nbsp;at /Users/jose/Documents/www/fake-api/node_modules/express/lib/router/index.js:281:22<br> &nbsp; &nbsp;at Function.process_params (/Users/jose/Documents/www/fake-api/node_modules/express/lib/router/index.js:335:12)<br> &nbsp; &nbsp;at next (/Users/jose/Documents/www/fake-api/node_modules/express/lib/router/index.js:275:10)<br> &nbsp; &nbsp;at /Users/jose/Documents/www/fake-api/node_modules/json-server/lib/server/defaults.js:55:5<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (/Users/jose/Documents/www/fake-api/node_modules/express/lib/router/layer.js:95:5)</pre>
</body>
</html>

Seems like is not able to get the email from the request. I'm not a node developer and I don't know my way around the code

I found the issue, on server.js lines 10 and 11 should be uncommented
https://github.com/techiediaries/fake-api-jwt-json-server/blob/master/server.js#L10-L11

is this working ?

I added those two lines in my server.js but still not working. same error.

what is the error you get ?

Actually i am following this
https://www.ylorenzana.rocks/nodejs-api-authentication-tutorial-pt-1/
When i call register with postman i get this error.
I added those two lines
server.use(bodyParser.urlencoded({extended: true}))
server.use(bodyParser.json())

even though i am getting same error.

I debug and found that res.body is undefined.

It is like this:
const { email, password } = req.body;
if (!isEmail(email)) {
throw new Error('Email must be a valid email address.');
}

i am sending data with body raw option in postman.

Error:
Cannot destructure property email of "undefined" or "null"

please re-install body-parser agian

npm audit fix

is this working

It is working but it is not parsing correctly. I am getting this in req.body

{ '{\n"email": "test@teet.com",\n"password": "tessdfst12dd3"\n}': '' }

That above thing i was gettig on using Raw option.
I am getting right things on x-www-form-urlencoded.
{ email: 'test@sdf.com', password: 'testisdlf' }

Thank you very much.

welcome

We have to put body Parser function on top of routes. Otherwise we may come on this error.

Getting the below error after fixing the destructure error by uncommenting line 10,11
http://localhost:3000/auth/login
{
"status": 401,
"message": "Incorrect email or password"
}
Could anyone help me fix it

I found the issue, on server.js lines 10 and 11 should be uncommented
https://github.com/techiediaries/fake-api-jwt-json-server/blob/master/server.js#L10-L11

Thanks, buddy i was looking from hours

TypeError: Cannot destructure property 'test' of 'req.body' as it is undefined.

thanks,wish had seen an hour earlier.

app.use(bodyParser.json()) // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true }))

I figured out that this code needs to be used before specifying route path.