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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With