简体   繁体   中英

Node.js with express request.body return undefined

So, i've been working on backend for my app. The error is in the request, when posting user data.

const db = require('../database/connection')

module.exports = {
    async create(req, resp) {
        console.log(req.body)
    }
}

The Terminal return undefined , but, I post this

{
    "name": "victor",
    "email": "victor",
    "password": "victor",
    "dtnasc": "1999/11/1",
    "sexo": "M",
    "altura": "1,8",
    "peso": "60"
}

I try to use Body-parser too, but it's still the same

PS: sorry for the english

Sounds like you don't have the body-parser middleware set up correctly.

Depending on the version of Express you are using you may have to install it separately.

npm install body-parser --save

If you do have it installed, but still don't find it working make sure to have your express app use the body-parser middleware before you define your routes.

It's a middleware so it will transform the req before passing it along but if you have it after you define your routes it will never be reached.

app.use(bodyParser.urlencoded({extended: true}))
// routes go below
app.get(...)

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