Comments (15)
https://github.com/koajs/koa/blob/master/lib/request.js#L168 ctx.query
will always point to the same object if ctx.querystring
is not changed. @kilianc can you try to display more informations like ctx.querystring
or ctx._querycache
?
from discussions.
i would do it this way:
ctx.query = {
sort: '-createdAt',
...ctx.query,
}
does that work?
from discussions.
Does object spread work without a transpiler?
It works with 8.4+ according to node.green
from discussions.
I am on 7.10 so I can't but I can Object.assign:
ctx.query = Object.assign({ sort: '-createdAt' }, ctx.query)
Or, looking at the getters setters behavior:
if (!ctx.query.sort) {
ctx.querystring = ctx.querystring || 'sort'
ctx.query.sort = '-createdAt'
}
It is still very unnecessary unexpected behavior
from discussions.
Which version of koa do you use and are there any middlewares you use before the code?
from discussions.
@likegun [email protected]
no middleware
from discussions.
PR welcomed
from discussions.
Tried to reproduce this using [email protected]
and [email protected]
with no luck so far. Wondering what am I doing wrong:
/app # npm ls
npm info it worked if it ends with ok
npm info using [email protected]
npm info using [email protected]
/app
`-- [email protected] extraneous
Source code:
const
http = require("http"),
Koa = require("koa"),
app = new Koa(),
PORT = 12345,
URL = `http://localhost:${PORT}`;
app.use(ctx => {
console.log(ctx.query)
if (!ctx.query.sort)
ctx.query.sort = "-createdAt"
console.log(ctx.query)
ctx.body = "Hello Koa";
});
app.listen(PORT);
http.get(URL, res => {
res.setEncoding("utf8");
let body = "";
res.on("data", data => {
body += data;
});
res.on("end", () => {
console.log(body);
process.exit(0);
});
});
Result:
{}
{ sort: '-createdAt' }
Hello Koa
from discussions.
failed to reproduce this with [email protected]
from discussions.
Failed to reproduce with [email protected], with Node.js v7.1.0, 7.10.0 and 7.10.1 (as suggested by the original reporter).
from discussions.
@kilianc Failed to reproduce too.Can you display the full code?
from discussions.
I've hit a similar issue:
ctx.query = { date: new Date() }
console.log(ctx.query) // prints { "date": "" }
This is likely because the setter is using querystring
which defaults all parameter types (like date) it does not support to an empty string https://nodejs.org/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options.
The workaround that works is to use Object.assign
Object.assign(ctx.query, { date: new Date() })
console.log(ctx.query) // prints the right value for the date
Koa 2.6.2.
Looking at https://github.com/koajs/koa/blob/ed84ee50da8ae3cd08056f944d061e00d06ed87f/lib/request.js#L172-L187
I guess the goal of the getter is to not parse the querystring unless it's being accessed but is there a reason to have the setter be overridden?
This is tagged as PR welcomed: hat change would you be open to?
from discussions.
i'm not sure why you all are trying to overwrite query
. i would just make a copy.
ctx.locals = {
...ctx.query,
date: new Date()
}
from discussions.
@jonathanong the problem here is that if fails silently. Can we Object.freeze
or put a proxy around it?
from discussions.
I’m okay with that. Wanna make a PR?
from discussions.
Related Issues (20)
- Improving the Koa ecosystem by moving all Koa modules to the koajs organization! HOT 62
- How to exclude api router? HOT 1
- Access to XMLHttpRequest at 'https://localhost:3443/images' from origin 'http://localhost:4200' has been blocked by CORS policy
- Destroy Mounts
- Conditional middleware
- Support for spa server?
- how to reg two part to the same file
- learning koa 2 and updating examples:https://github.com/aline2013/koa2-examples
- how to set interval within stream
- Hosting mp3 file
- Either 404 or wrong content HOT 1
- Error jump URL when ctx.path is directory
- koa-static: heavy caching issue HOT 2
- Why do i need koa-static HOT 1
- 404 not found for everything HOT 2
- koajs/conditional-get: uws.http failure
- Using both koa-compress and koa-static , extremely slow when visit static files from 127.0.0.1 or localhost (from public address is fast), why? HOT 2
- Brotli compress is slow HOT 2
- @fl0w becomes @miwnwski HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from discussions.