Can't send nested json object to node express tag parser

Hi I am creating a sample REST api using Node, Express and Mongo. I am using the average bodyParser () technique to parse form data. His working tone for a simple object says

         var user = {
             name:'test',
             age:'20'
         }

      

req.body creates the same set of formats to store in mongodb.

         {
             name:'test',
             age:'20'
         }

      

When using a complex object

         var user = {
                 name:'test',
                 age:'20',
                 education: {
                     institute:"xxx",
                     year:2010
                 }
            }

      

req.body creates a different format like

           {
                 name:'test',
                 age:'20',
                 education[institute]: "xxx",
                 edcuation[year]:2010
            }

      

I would like to receive the same format that I am sending in the body to store them in the database. Is this the correct approach or any other method available for this?

+3


source to share


1 answer


I think it's not clear about the documentation. I spent several hours looking for it. Anyway..

You should change the body-parser parameter to extended: true

as shown below.



app.use(bodyParser.urlencoded({ extended: true));

      

https://github.com/expressjs/body-parser?_ga=1.163627447.940445150.1418712389#bodyparserurlencodedoptions

+9


source







All Articles