Description
Node 6 changed the querystring.parse
to return an object whose __proto__
property no longer points back to Object
. This was done to allow for more esoteric query string names, such as https://example.com/route?__proto__=some-value
, which would conflict with the native js object properties.
However, this causes js-data to error when calling hasOwnProperty()
if you pass it the querystring
object.
This is especially problematic when using js-data-http as it automatically sends the requests with the url's query strings in the format js-data expects.
It can be fixed by using
Object.prototype.hasOwnProperty(query, propKey);
in place of
query.hasOwnProperty(propKey);
The libraries involved are
- api:
- node 5.11.1 // yikes
- node 6.17.1
- js-data 3.0.8
- koa 1.7.0
- webapp
- js-data 3.0.0-rc.4
- js-data-http 3.0.0-rc.2
It can also be fixed externally by shimming the hasOwnProperty()
function onto the querystring
exports.jsData = function() {
// koa 1.x middleware
return function*(next) {
this.jsData = {};
// `this.query` is the result of the `querystring.parse()` function
this.jsData.query = this.query;
function hasOwnPropertyShim(key) {
// `this` === `this.jsData.query`
return Object.hasOwnProperty.call(this, key);
}
// make the prop unenumerable, or it will get included in the sql query
Object.defineProperty(this.jsData.query, 'hasOwnProperty', {
value: hasOwnPropertyShim,
enumerable: false,
});
// ...
}
}
However, that solution isn't super intuitive without some digging