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

this.query no longer has hasOwnProperty method in node 6.2 #747

Closed
rickharrison opened this issue May 27, 2016 · 3 comments
Closed

this.query no longer has hasOwnProperty method in node 6.2 #747

rickharrison opened this issue May 27, 2016 · 3 comments

Comments

@rickharrison
Copy link

rickharrison commented May 27, 2016

My code uses hasOwnProperty to check for certain properties on this.query. In node 6.2, it no longer has this method (or as it seems with the following test case, no other methods). Here is a failing test case for this issue (this test is intended to be in /test/request/query.js):

it('should return an object with hasOwnProperty', function (){
  var ctx = context({ url: '/?page=2' });
  ctx.query.should.have.property('hasOwnProperty');
})
1 failing

  1) ctx.query should return an object with hasOwnProperty:
     TypeError: Cannot read property 'have' of undefined
      at Context.<anonymous> (test/request/query.js:29:21)

This test passes on node v5.6.0

@rickharrison
Copy link
Author

Never mind, it looks like this was intended: nodejs/node#6055

@PlasmaPower
Copy link
Contributor

Yep, it broke our tests (with should) too.

@crobinson42
Copy link

crobinson42 commented Sep 22, 2018

Work-around for folks looking into this

(ctx, next) => {
    if (ctx.request.query && typeof ctx.request.query === 'object') {
      // node.js removed the Object.prototype on querystring https://github.com/nodejs/node/pull/6055
      // coerce query to inherit from Object.prototype
      Object.defineProperty(ctx.request, 'query',{
        value: { ...ctx.request.query },
        writable: false
      });
      Object.defineProperty(ctx, 'query',{
        value: { ...ctx.request.query },
        writable: false
      });
    } 
   
   return next();
}

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

No branches or pull requests

3 participants