query-string icon indicating copy to clipboard operation
query-string copied to clipboard

TypeError: Cannot convert a Symbol value to a string

Open fengxinming opened this issue 4 years ago • 8 comments

I am not sure if the Symbol value would be ignored

const { stringify } = require('query-string');

stringify({ a: () => (1), b: Symbol('test') });
module.exports = str => encodeURIComponent(str).replace(/[!'()*]/g, x => `%${x.charCodeAt(0).toString(16).toUpperCase()}`);
                        ^

TypeError: Cannot convert a Symbol value to a string
    at encodeURIComponent (<anonymous>)
    at module.exports (/Users/jesse/workspace/iot-cloud-fe/iot-cloud-util/benchmark/node_modules/[email protected]@strict-uri-encode/index.js:2:25)
    at encode (/Users/jesse/workspace/iot-cloud-fe/iot-cloud-util/benchmark/node_modules/[email protected]@query-string/index.js:188:27)
    at /Users/jesse/workspace/iot-cloud-fe/iot-cloud-util/benchmark/node_modules/[email protected]@query-string/index.js:382:39
    at Array.map (<anonymous>)
    at exports.stringify (/Users/jesse/workspace/iot-cloud-fe/iot-cloud-util/benchmark/node_modules/[email protected]@query-string/index.js:361:14)
    at Object.<anonymous> (/Users/jesse/workspace/iot-cloud-fe/iot-cloud-util/benchmark/query-string.js:12:1)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)

fengxinming avatar Apr 16 '21 03:04 fengxinming

These are the supported values: https://github.com/sindresorhus/query-string/blob/20992772758fc74188db64e789535469adaf241f/index.d.ts#L398-L403

I guess we could improve the readme to mention those too, and also throw a more human-friendly error.

sindresorhus avatar Apr 16 '21 18:04 sindresorhus

@sindresorhus I feel like the Symbol value being ignored that would be better

fengxinming avatar Apr 17 '21 13:04 fengxinming

I disagree. That could hide bugs. I prefer strict type checking. This is also how the browser API works:

new URLSearchParams({foo: Symbol()})
//=> Uncaught TypeError: Cannot convert a Symbol value to a string

sindresorhus avatar Apr 17 '21 13:04 sindresorhus

@sindresorhus but other packages like qs and querystringify work well if give a Symbol value.

fengxinming avatar Apr 17 '21 15:04 fengxinming

That is not a good argument for doing it.

sindresorhus avatar Apr 17 '21 17:04 sindresorhus

@sindresorhus anyway, it's up to you

fengxinming avatar Apr 18 '21 06:04 fengxinming

@sindresorhus Yes you were right, Symbol should not be converted to string , from mdn web docs image

RiteshKSingh1709 avatar May 25 '21 05:05 RiteshKSingh1709

We are having a similar issue encodeURIComponent(...).replaceAll is not a function in:

TypeError: encodeURIComponent(...).replaceAll is not a function
  at strictUriEncode(../node_modules/query-string/base.js:8:62)
  at encode(../node_modules/query-string/base.js:256:27)
  at map(../node_modules/query-string/base.js:450:10)
  at Array.map(<anonymous>)
  at stringify(../node_modules/query-string/base.js:429:14)

It's only happening to users on Edge 122. A very strange errror.

The code is:

  const search = queryString.stringify({
    'redirect-to': `${location.pathname}${location.search}`,
  });

esetnik avatar Apr 05 '24 13:04 esetnik