-3

in express on node

var query = request.query;
print out the info of query below:
console result


add:

on mac: query instanceof Object is true
but on CentOS release 6.4 (Final) : is false(images below)

add again:
node: 0.12.2
express: 3.4.4
OS: CentOS release 6.4(Final)

SKing7
  • 2,204
  • 4
  • 21
  • 29
  • Can you do `console.log(query,__proto__ == Object.prototype)`? – Bergi May 06 '15 at 03:47
  • I'm trying to reproduce this issue in both Express 3 and Express 4 and I am unable to. There must be something peculiar in your code. Please provide more of your code! Thanks! – Trott May 06 '15 at 03:48
  • Have a look at [Does every object in JS have a toString() method?](http://stackoverflow.com/q/18640776/1048572) – Bergi May 06 '15 at 03:52
  • Typically when `foo instanceof Object === false`, the `foo` was created in such a way that does not share `Object.prototype`. For example `var foo = Object.create(null);` will create an object with no (Object) prototype. This also means it has no built-in methods anymore. However, I cannot find `Object.create(null)` or similar being used in Express or in any of its dependencies for querystring parsing. – mscdex May 06 '15 at 03:55
  • Additionally, re-assigning `foo.__proto__` to something other than `Object.prototype` will cause the `instanceof` check to return false. – mscdex May 06 '15 at 03:57
  • @mscdex: No: `var foo = {}; var bar = {}; foo.__proto__ = bar; foo instanceof Object;` – Felix Kling May 06 '15 at 04:15
  • @Bergi `console.log(query,__proto__ == Object.prototype)` true – SKing7 May 06 '15 at 04:17
  • @SKing7: Sorry, typo. I meant `console.log(query.__proto__ == Object.prototype)` which must not yield `true` – Bergi May 06 '15 at 05:18
  • @Trott there are only the read operation on request.query – SKing7 May 06 '15 at 05:40
  • @Bergi yes, i find the typo, it return true – SKing7 May 06 '15 at 05:42
  • @SKing7: But then `query instanceof Object` must return `true` as well? (Or otherwise there's a serious issue somewhere) – Bergi May 06 '15 at 05:43
  • @Bergi yes it is strange. – SKing7 May 06 '15 at 05:49
  • Well, if `query.__proto__ == Object.prototype && !(query instanceof Object)`, then that means a) someone played with `.__proto__` b) someone played with `.prototype` c) your engine has a bug. – Bergi May 06 '15 at 05:52
  • That may be the only place you do anything with `request.query` in your code, but there may be weirdness with the way `request` is handled, or there may be important middleware modifiying `request` before your middleware or any number of other things. Moreover, if there really is nothing else going on, then the code will be just a few lines and there's no reason not to show it. Without additional context, we are all wasting our time on this. – Trott May 06 '15 at 13:18

1 Answers1

1

This code displays true in both Express 3 and Express 4.

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send(req.query instanceof Object);
});

var server = app.listen(3000, function () {

  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);

});

So this does not seem to be a problem that is generally (or at least not universally) present in Express.

Most likely thing I can think of is that somewhere in your code (or in a library/middleware you are using), query.__proto__ is set to something that looks like an object but is not. Maybe something similar to this?:

var query = {};
query.__proto__ = null;
query.__proto__ = '{}';
console.log(query.__proto__); // {}
console.log(typeof query); // object
console.log(query instanceof Object); // false
Trott
  • 66,479
  • 23
  • 173
  • 212
  • Notice that `query.__proto__ = '{}';` does absolutely nothing (imo, it should `throw` in strict mode) – Bergi May 06 '15 at 04:01
  • It does nothing good, but it does do one thing bad: It causes a misleading message for `console.log(query.__proto__);`. I put it there to generate log messages that are consistent with what the OP is reporting. – Trott May 06 '15 at 04:08
  • @Trott: Ah I see, it's no longer a setter there. – Bergi May 06 '15 at 05:53