Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node JS get request body length

Tags:

node.js

I need to get the length of a field in req.body. But using req.body.username.length <= 5 I got error Cannot read property 'length' of undefined. How can I do it?

like image 656
CherryBelle Avatar asked Oct 26 '25 08:10

CherryBelle


1 Answers

Consider that objects don't has property length in JavaScript. For this purpose you can use this code instead.

Object.keys(req.body).length

Object.keys(req.body) will return an array of all keys. So, we can simply get the length of an array with .length.

like image 71
Aryan Ahadinia Avatar answered Oct 28 '25 22:10

Aryan Ahadinia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!