Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not post the nested object json to node express body parser

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?

like image 297
bleedCoder Avatar asked Nov 03 '14 09:11

bleedCoder


1 Answers

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

like image 53
efkan Avatar answered Oct 17 '22 18:10

efkan