tj / node-querystring

querystring parser for node and the browser - supporting nesting (used by Express, Connect, etc)
MIT License
455 stars 66 forks source link

`hasOwnProperty` no longer on parsed object #61

Closed nicolaasmatthijs closed 11 years ago

nicolaasmatthijs commented 11 years ago

I'm using node-querystring as part of Express.js. After updating to the latest version of Express, the request body object passed into POST requests no longer has the hasOwnProperty function. I think I've been able to narrow this down to the changes made in #58 .

serv.post('/api/test', function(req, res) {
    if (req.body.hasOwnProperty('test') {
        --> Error: req.body has no method hasOwnProperty. The body is {'test': 'test'}
    }
});

Was this a deliberate change? If so, can you explain the rationale behind it? Thanks!

eivindfjeldstad commented 11 years ago

It's was done to prevent the request body from messing with global Object.prototype. Have a look at https://github.com/visionmedia/node-querystring/blob/master/test/parse.js#L154 Node is such a nightmare sometimes. haha

tj commented 11 years ago

yeah I thought this may break some people's tests as well if they're using Object#should with should.js, but overall it's a necessary change

jbarros35 commented 9 years ago

where is the solution for this?

velipso commented 7 years ago

@jbarros35 and anyone else...

I got around this via a silly hack:

// body.hasOwnProperty doesn't exist
body = JSON.parse(JSON.stringify(body));
// body.hasOwnProperty now exists
velipso commented 7 years ago

nvm, having to replace all my code that uses hasOwnProperty with something like this instead:

function has(obj, key){
    return Object.prototype.hasOwnProperty.call(obj, key);
}

Thanks, Obama.