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

6.x querystring.parse returns object for which instanceof Object is false (was true for 4.x) #8737

Closed
kosmjon opened this issue Sep 23, 2016 · 8 comments
Labels
querystring Issues and PRs related to the built-in querystring module.

Comments

@kosmjon
Copy link

kosmjon commented Sep 23, 2016

  • Version: 6.3.1 and also 6.6.0
  • Platform: Windows 7 x64 and also Centos 64 x86
    Linux ip-10-0-0-35.localdomain 3.10.0-229.14.1.el7.x86_64 deps: update openssl to 1.0.1j #1 SMP Tue Sep 15 15:05:51 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
  • Subsystem: querystring

Here is a snippet that demos the problem:

var params, qs = require('querystring');
params = qs.parse('languageId=en-US');
console.log('%j %s', params, typeof params);
if (params instanceof Object) {
  console.log('params instanceof Object is true');
} else {
  console.log('params instanceof Object is false');
}

On 4.4.4 prints:
{"languageId":"en-US"} object
params instanceof Object is true
On 6.3.1 and 6.6.0 prints:
{"languageId":"en-US"} object
params instanceof Object is false

@targos targos added the querystring Issues and PRs related to the built-in querystring module. label Sep 23, 2016
@targos
Copy link
Member

targos commented Sep 23, 2016

This was an intended change. The object returned by querystring.parse inherits from null instead of Object since v6.0.0.

See #6055 and https://github.com/nodejs/node/wiki/Breaking-changes-between-v5-and-v6#querystring

@imyller
Copy link
Member

imyller commented Sep 23, 2016

Basicly this happens because typeof Object.create(null) is object, but Object.create(null) instanceof Object evaluates to false.

@claudiorodriguez
Copy link
Contributor

You should check for params !== null && typeof params === 'object' (typeof null is 'object')

@imyller
Copy link
Member

imyller commented Sep 23, 2016

So, Object.create(null) does not inherit any properties of Object and thus is not instanceof Object.

querystring.parse() now returns this kind of "object".

As @claudiorodriguez pointed out you should use typeof

@kosmjon
Copy link
Author

kosmjon commented Sep 23, 2016

The affected code is in a 3rd-party module which I guess we will have to pull into our project to fix. This seems like a very silly code-breakage that would have been an easy fix in querystring.parse to make it behave as before. Thanks for the information.

@kosmjon kosmjon closed this as completed Sep 23, 2016
@bnoordhuis
Copy link
Member

This seems like a very silly code-breakage that would have been an easy fix in querystring.parse to make it behave as before.

Something tells me you didn't actually read through #6055.

@kosmjon
Copy link
Author

kosmjon commented Sep 23, 2016

You are correct. Sorry about that. I get it now. Thanks to everyone that commented and helped me to understand.

@bnoordhuis
Copy link
Member

No problem, glad it got cleared up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
querystring Issues and PRs related to the built-in querystring module.
Projects
None yet
Development

No branches or pull requests

5 participants