expressjs / body-parser

Node.js body parsing middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting string length value wrong in req.body values.

parthpatwala opened this issue · comments

I'm trying to pass one string from POST request. consider like this. I'm using Postman.

{
  "phone": "11111111111"
}

Now in backend side with use of body parser, I can get this value by req.body.phone. Which I'm getting correctly.

But the string length of this variable is always 1 more than the actual one. Now current examples value should be 11 in the console but I'm getting 12. because of that, comparison with static number is false. However static variable length gives me 11.
After looping the value, I found that I'm getting an extra empty string in the value which I can't see in the normal console. Which is weird.

How can solve this problem?

Did you paste that body into postman? Try deleting it and fully typing out the entire body in postman and trying again. The invisible character is likely in your postman.

I was not able to reproduce using postman and the body you showed above; req.body.phone was length of 11.