2016-09-23 126 views
1

我要发布嵌套支柱件的NodeJS - 快 - 嵌套后数据未解析正确

查看HTML形式

<form method="post" action=""> 
<input type="text" placeholder="Enter Tag here" class="gui-input" name="reply_message"> 
<input type="text" placeholder="Enter Label for Decision" class="gui-input" name="decision[0][label]" value="Interested"> 
<input type="text" placeholder="Enter decision keywords here" class="gui-input" data-role="tagsinput" name="decision[0][keyword]" value="Interested, call me"> 
<input type="text" placeholder="Enter Label for Decision" class="gui-input" name="decision[1][label]" value="Not Interested"> 
<input type="text" placeholder="Enter decision keywords here" class="gui-input" data-role="tagsinput" name="decision[1][keyword]" value="not interested, not"> 
<input type="text" placeholder="Enter Label for Decision" class="gui-input" name="decision[2][label]" value="Call Later" > 
<input type="text" placeholder="Enter decision keywords here" class="gui-input" data-role="tagsinput" name="decision[2][keyword]" value="Interested, call me, later"> 
<button class="button btn-primary" type="submit">Submit </button> 

路由器文件

router.post('/mapping', isLoggedIn, function (req, res, next) { 
    var posted_data= req.body; 
    console.log(req.body); 
    res.send(posted_data); 
}); 

我得到像这样的发布数据结构

{ 
    "reply_message": "This is test", 
    "decision[0][label]": "Interested", 
    "decision[0][keyword]": "Interested, call me", 
    "decision[1][label]": "Not Interested", 
    "decision[1][keyword]": "not interested, not", 
    "decision[2][label]": "Call Later", 
    "decision[2][keyword]": "Interested, call me, later" 
} 

但实际发布的数据结构应该是

{ 
    "reply_message": "This is test", 
    "decision": [{ 
     "label": "Interested", 
     "keyword": "Interested, call me" 
    }, { 
     "label": "Not Interested", 
     "keyword": "not interested, not" 
    }, { 
     "label": "Call Later", 
     "keyword": "Interested, call me, later" 
    }] 
} 

因此,如何CA我做到这一点,是否有任何节点模块我必须使用像这样发布表单数据?

+1

您使用的是旧版本Express和/或'身体parser'的? 'body-parser'的默认配置会自动解析该输入。请参阅['extended'选项](https://www.npmjs.com/package/body-parser#extended)。 – robertklep

+0

我正在使用最新版本的两个...我必须使用扩展选项来以嵌套方式解析。 –

+0

我正在链接的那个,这是默认的。但是,由于表单的“操作”是空的,我假设您正在使用某种客户端代码来提交数据?你确定这样做没问题吗?例如,你没有提交任何机会的数据作为JSON? – robertklep

回答

1

那么,name="decision[0][label]"工作正常。表单数据作为键值对提交,输入名称成为关键字。

如果使用HTTP GET提交表单,则会在req.query中获得所需的对象。但对于HTTP POST,它按原样出现在req.body中。

这里,qs module可以帮你在服务器端:

const qs = require('qs'); 

const input = { 
    "reply_message": "This is test", 
    "decision[0][label]": "Interested", 
    "decision[0][keyword]": "Interested, call me", 
    "decision[1][label]": "Not Interested", 
    "decision[1][keyword]": "not interested, not", 
    "decision[2][label]": "Call Later", 
    "decision[2][keyword]": "Interested, call me, later" 
}; 

const output = qs.parse(qs.stringify(input)); 

console.log(output); 

// console: 
{ reply_message: 'This is test',                         
    decision:                              
    [ { label: 'Interested', keyword: 'Interested, call me' },                 
    { label: 'Not Interested', keyword: 'not interested, not' },                
    { label: 'Call Later', keyword: 'Interested, call me, later' } ] } 
+0

感谢您的帮助...其工作...: ) –