javascript - req.body automatically cast array to object after 20 entries -
in form can add many properties want dynamically:
<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>
in node.js, want foreach
on req.body.properties
more 20 properties, req.body.properties
somehow cast in object
, foreach
function not exist anymore.
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); ... }
here result 20 properties or less :
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' } ]
and more 20 properties :
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' } }
i understand why node.js have behavior , how avoid it. can me ?
ps : had reduce code example because stackoverflow not having more code explanation. hope still understandable.
body-parser
(which handles request bodies) uses qs
library parse url-encoded data.
the documentation library states, arrays:
qs limit specifying indices in array maximum index of 20. array members index of greater 20 instead converted object index key
...
this limit can overridden passing
arraylimit
option
however, there doesn't seem way of passing option qs
through body-parser
. it's relatively easy convert object array:
post.properties.length = object.keys(post.properties).length; let properties = array.from(post.properties);
Comments
Post a Comment