要求的事情.主体在20个条目后自动将数组转换为对象
req.body automatically cast array to object after 20 entries
在我的表单中,我可以动态地添加任意多的属性:
<form action="">
<div class="property">
<label>Name : <input type="text" name="properties[1][name]"></label>
<label>Order : <input type="text" name="properties[1][order]"></label>
<label>Label : <input type="text" name="properties[1][label]"></label>
<label>Type : <input type="text" name="properties[1][type]"></label>
<label>Description : <textarea name="properties[1][description]"></textarea></label>
</div>
<div class="property">
<label>Name : <input type="text" name="properties[2][name]"></label>
<label>Order : <input type="text" name="properties[2][order]"></label>
<label>Label : <input type="text" name="properties[2][label]"></label>
<label>Type : <input type="text" name="properties[2][type]"></label>
<label>Description : <textarea name="properties[2][description]"></textarea></label>
</div>
<div class="property">
<label>Name : <input type="text" name="properties[3][name]"></label>
<label>Order : <input type="text" name="properties[3][order]"></label>
<label>Label : <input type="text" name="properties[3][label]"></label>
<label>Type : <input type="text" name="properties[3][type]"></label>
<label>Description : <textarea name="properties[3][description]"></textarea></label>
</div>
...
</form>
在node.js中,我想在req.body.properties
上做forEach
,但有超过20个属性,req.body.properties
以某种方式在Object
中转换,forEach
功能不再存在。
propUpdate: (req, res) => {
const post = req.body;
console.log('isArray :', Array.isArray(post.properties));
console.log('isObject :', require('lodash').isObject(post.properties));
console.log('properties :', post.properties);
...
}
下面是包含20个或更少属性的结果:
isArray : true
isObject : true
properties : [ { name: 'test 1',
order: '1',
label: 'label 1',
type: 'type 1' },
{ name: 'test 2',
order: '2',
label: 'label 2',
type: 'type 2' },
....
{ name: 'test 20',
order: '20',
label: 'label 20',
type: 'type 20' } ]
并具有20多个属性:
isArray : false
isObject : true
properties : { '1' :
{ name: 'test 1',
order: '1',
label: 'label 1',
type: 'type 1' },
'2' :
{ name: 'test 2',
order: '2',
label: 'label 2',
type: 'type 2' },
....
'21' :
{ name: 'test 21',
order: '21',
label: 'label 21',
type: 'type 21' } }
我想了解为什么node.js有这种行为以及如何避免它。有人能帮我吗?
PS:我不得不减少我的代码示例,因为stackoverflow不喜欢有比解释更多的代码。
body-parser
(处理请求体)使用qs
库解析url编码的数据。
该库的文档声明,关于数组:
qs还将限制在数组中指定索引的最大索引数为20。任何索引大于20的数组成员都将被转换为索引为键
的对象。…
这个限制可以通过传递一个
arrayLimit
选项来覆盖
然而,似乎没有办法通过body-parser
将该选项传递给qs
。但是将对象转换为数组相对容易:
post.properties.length = Object.keys(post.properties).length;
let properties = Array.from(post.properties);
相关文章:
- 将数组对象传递到struts2中的操作类
- 如何使用jquery返回php-json数组对象
- 从其名称获取javascript数组对象
- 值未与数组对象绑定
- 如何按数组/对象值的倍数过滤对象数组
- 在表中显示数组对象
- 在数组对象中分组
- 将第二个或多个数组推送到数组对象 AngularJS 中
- 重新排列 JavaScript 数组/对象
- 键上的javascript数组对象过滤器
- handontable:在不更改数据数组/对象的情况下隐藏某些列
- 用于跟踪购物车可用性的Javascript数组/对象/哈希表
- 重建有角度的java脚本数组对象
- 对JSON数组对象进行排序
- 连接与数组对象相关的文本:方式和位置
- 哪些浏览器和版本支持将常见的类数组对象直接传递到fn.apply()
- 如何使用Handlebars循环数组对象和模板
- 是否可以引用JS数组/对象中的另一个元素
- Javascript中的名称索引-数组/对象
- 数组长度不等于数组对象