node.js - Nodejs - Express - 嵌套发布数据无法正确解析

标签 node.js express

我想发布带有嵌套 post 元素的表单

以 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"
    }]
}

那么我如何实现这一点,是否有任何 Node 模块我必须用于这样的发布表单数据?

最佳答案

嗯,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' } ] } 

关于node.js - Nodejs - Express - 嵌套发布数据无法正确解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39654207/

相关文章:

node.js - 使用 Webpack 构建后端 Express/WS Node 应用程序时的警告

node.js - 在 Node js 中启动 Express 对象有什么区别?

node.js - 如何以 Angular 显示 Node 服务器上保存的图像

javascript - 验证node.js中的请求并在错误时退出

node.js - sequelize belongsToMany 给出 TypeError : undefined is not a function

reactjs - 用于服务器端渲染的 create-react-app typescript

node.js - Auth0 和 Express-openid-connect/回调没有响应

node.js - Typescript+Node.js 中的 os.EOL

javascript - 在 Javascript 中,嵌入一组 Promise 时 setTimeout 会阻塞吗?

javascript - 当客户端不在同一 IP 上时,无法通过客户端调用 Node 服务器