Skip to content
Advertisement

Getting TypeError: Cannot read property ‘name’ of undefined, while posting the form – node.js

I am building a node Js project and i am saving the values of form to a mongoDB database. Despite of trying i couldn’t find what is causing this error. Error is at router.post function on 3rd line.

Please guide me through this through your magical powers of coding and debugging. 😀

JavaScript

you can see clearly I have defined the newEmployee Object, so why ‘name’ is the property of undefined.

JavaScript

Advertisement

Answer

It doesn’t look like you’re using a body parser. Without one, req.body will always be undefined, which looks like your issue. Try putting this before you define any of your routes.

JavaScript

Edit: Also, make sure that you use the body parser middleware before your router.

JavaScript

Docs

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement