question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Koa 2 ctx.query nesting

See original GitHub issue
// When GET /path?a[b]=c
// I think `ctx.query ` should be:
{
   a:{
      b:'c'
   }
}
// But it is
{
   'a[b]':c
}

// Is this a bug?

// if it is, I think
// koa/lib/request.js line 11
const qs = require('querystring');
// should replaced by
const qs = require('qs');

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lourdcommented, Mar 25, 2016

Alternatively, you can use the qs module yourself in a middleware and put the parsed result on the context’s state, i.e. ctx.state.query = qs.parse(ctx.querystring). That’s what I do in my projects.

0reactions
demisxcommented, Jan 18, 2019

I second @lourd’s approach and would use qs directly instead of going through another npm dependency, especially since it’s not being actively maintained:

app.use(async (ctx, next) => {
  ctx.state.query = qs.parse(ctx.querystring)
  await next()
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Koa - next generation web framework for node.js
Set query-string to the given object. Note that this setter does not support nested objects. ctx.query = { next: '/login' }; ...
Read more >
Getting Request Data with Koa. Query string, originating IPs ...
The ctx.request.query property is also a setter, so we can set it to something else. However, it doesn't support nested objects.
Read more >
Building a RESTful API with Koa and Postgres - Michael Herman
This function takes the Koa context as a parameter, ctx . It's worth noting that this object encapsulates both the Node request and...
Read more >
Resolvers - Apollo GraphQL Docs
We want to define resolvers for the numberSix and numberSeven fields of the root Query type so that they always return 6 and...
Read more >
next generation web framework for node.js
Koa requires node v7.6.0 or higher for ES2015 and async ... This is useful for adding properties or methods to ctx to be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found