Skip to content
Advertisement

node js async/await : why i can’t get the data ? req.body

i am learning about async/await at Node.js to make a restful api and I got a problem in the PUT and PATCH method, where for req.body it can’t display the data that I want

here’s the code: controllers/users

JavaScript

and this code for router:

JavaScript

when I enable mongoose debug, only the findone function is active, and this method works on GET and POST.

i using :

JavaScript

i already set bodyparser middleware in my app.js.. but still won’t work for PATCH and PUT methods 🙁

please help me. I’m stuck. thank you

Advertisement

Answer

Looks like you arent corectly populating req.body with bodyParser

This is taken from the express website


req.body

Contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as body-parser and multer.

The following example shows how to use body-parsing middleware to populate req.body.

JavaScript

Take note of:

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