无法将嵌套对象json发布到node express body解析器

ble*_*der 3 mongodb node.js express body-parser

嗨,我正在使用Node,Express和Mongo创建示例REST api.我正在使用bodyParser()中间件来解析表单数据.它的工作正常,简单的对象说

         var user = {
             name:'test',
             age:'20'
         }
Run Code Online (Sandbox Code Playgroud)

req.body生成相同的格式集以将其保存在mongodb中.

         {
             name:'test',
             age:'20'
         }
Run Code Online (Sandbox Code Playgroud)

使用复杂对象时

         var user = {
                 name:'test',
                 age:'20',
                 education: {
                     institute:"xxx",
                     year:2010
                 }
            }
Run Code Online (Sandbox Code Playgroud)

req.body产生不同的格式

           {
                 name:'test',
                 age:'20',
                 education[institute]: "xxx",
                 edcuation[year]:2010
            }
Run Code Online (Sandbox Code Playgroud)

我希望得到我在正文中发布的相同格式,以便将它们保存在数据库中.这是正确的方法还是可用于此的任何其他方法?

efk*_*kan 9

我想,在文件上还不清楚.我花了好几个小时找到它.无论如何..

你应该改变你的身体解析器选项,extended: true如下所示.

app.use(bodyParser.urlencoded({ extended: true));
Run Code Online (Sandbox Code Playgroud)

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