GithubHelp home page GithubHelp logo

Immutable empty query obj about discussions HOT 15 OPEN

koajs avatar koajs commented on May 7, 2024
Immutable empty query obj

from discussions.

Comments (15)

dead-horse avatar dead-horse commented on May 7, 2024 1

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.

jonathanong avatar jonathanong commented on May 7, 2024

i would do it this way:

ctx.query = {
  sort: '-createdAt',
  ...ctx.query,
}

does that work?

from discussions.

fl0w avatar fl0w commented on May 7, 2024

Does object spread work without a transpiler?

It works with 8.4+ according to node.green

from discussions.

kilianc avatar kilianc commented on May 7, 2024

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.

likegun avatar likegun commented on May 7, 2024

Which version of koa do you use and are there any middlewares you use before the code?

from discussions.

kilianc avatar kilianc commented on May 7, 2024

@likegun [email protected] no middleware

from discussions.

jonathanong avatar jonathanong commented on May 7, 2024

PR welcomed

from discussions.

garcia556 avatar garcia556 commented on May 7, 2024

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.

qianL93 avatar qianL93 commented on May 7, 2024

failed to reproduce this with [email protected]

from discussions.

gaomd avatar gaomd commented on May 7, 2024

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.

likegun avatar likegun commented on May 7, 2024

@kilianc Failed to reproduce too.Can you display the full code?

from discussions.

benbabic avatar benbabic commented on May 7, 2024

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.

jonathanong avatar jonathanong commented on May 7, 2024

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.

kilianc avatar kilianc commented on May 7, 2024

@jonathanong the problem here is that if fails silently. Can we Object.freeze or put a proxy around it?

from discussions.

jonathanong avatar jonathanong commented on May 7, 2024

I’m okay with that. Wanna make a PR?

from discussions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.