Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot convert a Symbol value to a string #312

Open
fengxinming opened this issue Apr 16, 2021 · 8 comments
Open

TypeError: Cannot convert a Symbol value to a string #312

fengxinming opened this issue Apr 16, 2021 · 8 comments

Comments

@fengxinming
Copy link

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/_strict-uri-encode@2.0.0@strict-uri-encode/index.js:2:25)
    at encode (/Users/jesse/workspace/iot-cloud-fe/iot-cloud-util/benchmark/node_modules/_query-string@7.0.0@query-string/index.js:188:27)
    at /Users/jesse/workspace/iot-cloud-fe/iot-cloud-util/benchmark/node_modules/_query-string@7.0.0@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/_query-string@7.0.0@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)
@sindresorhus
Copy link
Owner

These are the supported values:

query-string/index.d.ts

Lines 398 to 403 in 2099277

export type Stringifiable = string | boolean | number | null | undefined;
export type StringifiableRecord = Record<
string,
Stringifiable | readonly Stringifiable[]
>;

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

@fengxinming
Copy link
Author

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

@sindresorhus
Copy link
Owner

sindresorhus commented Apr 17, 2021

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

@fengxinming
Copy link
Author

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

@sindresorhus
Copy link
Owner

That is not a good argument for doing it.

@fengxinming
Copy link
Author

@sindresorhus anyway, it's up to you

@RiteshKSingh1709
Copy link

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

@esetnik
Copy link
Contributor

esetnik commented Apr 5, 2024

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}`,
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants