question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When an array has more than 21 items, it is parsed from a url string into an object, instead of array

See original GitHub issue

Following an error that array.includes is not a function, it turns out that when an array which is longer than 21 items is translated into a url query string (using qs.stringify), and than the url string is parsed (using qs.parse) it returns an object, not an array. This can obviously cause issues when you expect an array and it’s methods.

console.log( typeof(qs.parse(qs.stringify({activities:[....array 20 items long]})) ) // Array
console.log( typeof(qs.parse(qs.stringify({activities:[....array 22 items long]})) ) // Object

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
jmikrutcommented, Feb 10, 2022

@ljharb — I can give some more info here.

@samicodesit is hitting the same issue that I am. Although arrayLimit is an option for qs.parse, it is not an option for qs.stringify. So, when we attempt to stringify an array with more than 20 items in the array, the function appears to use arrayFormat: 'indices' instead of the desired ``arrayFormat: ‘brackets’`.

We just need an option to specify an arrayLimit in the stringify method.

Also, THANK YOU for this package. We are heavily using qs in Payload CMS and have just run into this same need.

UPDATE:

I just found that this was actually not caused by the qs.stringify method on our frontend app—instead, it was caused by the qs-middleware package that we used on the backend, which was responsible for parsing incoming query strings. I needed to set the arrayLimit in that middleware, in order to parse large arrays. I thought it was the frontend stringify but it was actually the backend.

@samicodesit you might be having the same issue?

2reactions
ljharbcommented, Jul 24, 2019

This is the arrayLimit option, which is documented in the readme.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to convert URL parameters to a JavaScript object?
This is a BAD solution when you have an array field within your query params. In a case where you have ids=1&ids=2, you'd...
Read more >
JavaScript Split – How to Split a String into an Array in JS
The split() method splits (divides) a string into two or more substrings depending on a splitter (or divider). The splitter can be a...
Read more >
eval() - JavaScript - MDN Web Docs - Mozilla
The eval() function evaluates JavaScript code represented as a string and returns its completion value. The source is parsed as a script.
Read more >
URL | Node.js v19.3.0 Documentation
In accordance with browser conventions, all properties of URL objects are implemented as getters and setters on the class prototype, rather than as...
Read more >
http_build_query - Manual - PHP
If data is an array, it may be a simple one-dimensional structure, or an array of arrays (which in turn may contain other...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found