Hi I'm creating sample REST api using Node, Express and Mongo. I'm using bodyParser() middle ware to parse the form data. Its working fine for simple object say
var user = {
name:'test',
age:'20'
}
req.body produce the same set of format to save it in the mongodb like.
{
name:'test',
age:'20'
}
When using complex object
var user = {
name:'test',
age:'20',
education: {
institute:"xxx",
year:2010
}
}
req.body produce different format something like
{
name:'test',
age:'20',
education[institute]: "xxx",
edcuation[year]:2010
}
I would like to get the same format which i post in the body to save them in the database. Is this the right approach or any other method available to this?
I think, it's not clear on the documenatation. I've spent hours to find it. Anyway..
You should change your body-parser option to extended: true
like the below.
app.use(bodyParser.urlencoded({ extended: true));
https://github.com/expressjs/body-parser?_ga=1.163627447.940445150.1418712389#bodyparserurlencodedoptions
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