Home > Mobile >  Cannot read properties of undefined (reading 'email')
Cannot read properties of undefined (reading 'email')

Time:04-25

I'm facing this Type Error :

Cannot read properties of undefined (reading 'email').

The code runs perfectly but whenever I try to send payload using postman/thunderclient. it sends this error. I'm trying to make login signup using mongoDB and express js.

enter image description here

CodePudding user response:

The code runs perfectly but whenever I try to send payload using postman/thunderclient. it sends this error.

I think by payload you mean request body right. Error "Cannot read properties of undefined (reading 'email')" means that req.body is undefined, therefore you cannot access req.body.email.

Reason why req.body is undefined is probably because you didn't properly define request body in postman/thunderclient (I'm not sure, because you didn't provide enough details about the request.)

You can fix this error for example with using optional chaining req.body?.email (this way, code won't continue to read email when req.body is undefined), but beware, then the email key in function User.findOne will have value of undefined. However, I strongly recommend using some kind of validation beforehand and sending for example status code 400 when required keys in request body are missing.

CodePudding user response:

enter image description here

As per you image and error that you not pass the email in body row (JSON formate) data. You have to pass parameter in body row data.

I am attached the example of postman.postman sample image

  • Related