Pass array of objects in query string node.js
Solution 1
With the qs
module, you can get the object you're looking for if you use this format:
or[0][foo]=bar&or[1][bar]=baz
Solution 2
You can use options allowDots.
const stringParams = qs.stringify(params, {allowDots:true});
// myArray[0].name=MeWhit
qs.parse(stringParams , {allowDots: true});
// [{ name: MeWhit}]
Rob Allsopp
I love to code! I am perfectly at home working out front in React or Angular, or behind the scenes in Node or Go. I have also been known to dabble in machine learning and digital signal processing from time to time. I have always been a huge science/space nerd, and recently I have taken a slight detour from web development and begun pursuing a master's degree in scientific computing. I hope to eventually write software in the aerospace industry.
Updated on June 05, 2022Comments
-
Rob Allsopp almost 2 years
If I want the Node.js query parser to parse an array, I can send this:
'?or=foo&or=bar' // gets me { or: ['foo', 'bar'] }
If I want an object I can do this:
'?or[foo]=bar' // gets me { or: {foo: 'bar'}}
But how do I get an array of objects? I'd like this output:
{ or: [{foo: 'bar'}, {bar: 'baz'}]}
-
Rob Allsopp over 9 yearsWow that worked without the module actually. Thanks!