简体   繁体   中英

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

 replaceUser: async (req, res, next) => {
    //enforce that req.body must contain all the fields
    const { userId } = req.params;
    const  newUser  = req.body;
    // const result = await User.findByIdAndUpdate(userId, newUser, {new: true}).exec();
    // console.log(result)
    console.log(newUser)
    console.log(userId)

    // res.status(200).json(result);
    // console.log(userId, newUser)
},

and this code for router:

router.route('/:userId')
.get(UsersController.getUser)
.put(UsersController.replaceUser)
.patch(UsersController.updateUser)

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

i using :

    "body-parser": "^1.18.3",
    "express": "^4.16.3",
    "express-promise-router": "^3.0.3",
    "mongoose": "^5.3.1",

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

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.

var app = require('express')();
var bodyParser = require('body-parser');

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded


replaceUser: async (req, res, next) => {
    //enforce that req.body must contain all the fields
    const { userId } = req.params;
    const  newUser  = req.body;
    // const result = await User.findByIdAndUpdate(userId, newUser, {new: true}).exec();
    // console.log(result)
    console.log(newUser)
    console.log(userId)

    // res.status(200).json(result);
    // console.log(userId, newUser)
}

Take note of:

 app.use(bodyParser.json()); // for parsing application/json app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

[SOLVED] finally I got my data from req.body.. the problem is.. I forgot checked my headers in "application/json" in postman..

im sorry guys.. take your time to help my issue :)

I had the same issue, and tried all the answers from stackoverflow but nothing worked. finally I found that I was sending the data in text-form which supposed to be in json. so, if you're sure that everything you doing is correct, check body section in postman and change data type to json.

Nodejs doesn't recognise the req.body parameters, for that to work you need to install body-parser to your project. https://www.npmjs.com/package/body-parser

there is couple of examples in the link.

Did you used body-parser to read the request body? If not, use below lines..

const bodyParser = require('body-parser');

app.use(bodyParser.json());

Now you can read the body by req.body

You can use express.json()

const app = express();
app.use(express.json());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM