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

ctx.query is not a plain object while ctx.params and ctx.request.body is #1472

Closed
raphaelsoul opened this issue Jun 16, 2020 · 5 comments
Closed

Comments

@raphaelsoul
Copy link

image

As screenshot shows that ctx.query has no method hasOwnProperty from prototype

Which causes some package depends on ctx.query.hasOwnProperty calling an undefined method.

I am using koa@2.11.0

@cmidkiff87
Copy link

(Not a koajs dev, but had the same issue)

This is intended behavior as of node 6.
To get around it, you can use this instead

Object.hasOwnProperty.call(ctx.query, 'prop_name_here');

If you can't do that (such as using a library), you can shim it instead

app.use(async (ctx, next) => {

  ctx.query.hasOwnProperty = function(key) {
    // `this` === `ctx.query`
    return Object.hasOwnProperty.call(this, key);
  }
});

If that causes issues with object enumeration (let key of ctx.query erroring on 'hasOwnProperty'), you can make it unenumerable

app.use(async (ctx, next) => {

  function hasOwnPropertyShim(key) {
    // `this` === `ctx.query`
    return Object.hasOwnProperty.call(this, key);
  }

  Object.defineProperty(ctx.query, 'hasOwnProperty', {
    value: hasOwnPropertyShim,
    enumerable: false,
  });
});

@raphaelsoul
Copy link
Author

raphaelsoul commented Jun 22, 2020

(Not a koajs dev, but had the same issue)

This is intended behavior as of node 6.
To get around it, you can use this instead

Object.hasOwnProperty.call(ctx.query, 'prop_name_here');

If you can't do that (such as using a library), you can shim it instead

app.use(async (ctx, next) => {

  ctx.query.hasOwnProperty = function(key) {
    // `this` === `ctx.query`
    return Object.hasOwnProperty.call(this, key);
  }
});

If that causes issues with object enumeration (let key of ctx.query erroring on 'hasOwnProperty'), you can make it unenumerable

app.use(async (ctx, next) => {

  function hasOwnPropertyShim(key) {
    // `this` === `ctx.query`
    return Object.hasOwnProperty.call(this, key);
  }

  Object.defineProperty(ctx.query, 'hasOwnProperty', {
    value: hasOwnPropertyShim,
    enumerable: false,
  });
});

hmmm indeed. But I am running in node v10.20.1.
Currently i did like this validate({ ...ctx.query }) as a workaround

@cmidkiff87
Copy link

Node 10 has the same behavior.

Specifically

The object returned by the querystring.parse() method does not prototypically inherit from the JavaScript Object. This means that typical Object methods such as obj.toString(), obj.hasOwnProperty(), and others are not defined and will not work.

@dead-horse
Copy link
Member

@dead-horse
Copy link
Member

Close for now. If you have any other doubts you can continue the discussion. :)

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