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

request.query behaves immutable in 5.x #2752

Closed
mikegwhit opened this issue Sep 10, 2015 · 5 comments
Closed

request.query behaves immutable in 5.x #2752

mikegwhit opened this issue Sep 10, 2015 · 5 comments
Assignees

Comments

@mikegwhit
Copy link

Tested against 4.13, request.query is tested false for Object.isFrozen and also does not pass this test assuming HTTP GET:

app.all("*",function(req,res,next){
req.query.apples = "bananas";
assert(req.query.apples=="bananas")
});
@dougwilson
Copy link
Contributor

Hi!

Yes, this is expected behavior and is one of the breaking changes, listed in the History file as follows:

req.query is now a getter instead of a plain property

In Express 5, req.query is now a getter that only reflects the contents on req.url. This means, depending on what you are trying to achieve, you'll have to change your code. The following is a few possibilities, all depending on what you are trying to achieve (let me know if these are not one, as I'm just guessing):

  1. If you are trying to alter the query for transparent processing, you need to alter the req.url itself. This is so the change is truly transparent: no matter if the later code is looking in req.query or parsing the query string of req.url, it will see the updated query.
  2. If you are simply trying to track some kind of state, you should not use req.query for tracking, rather read out of req.query and into any req.* property of your choosing.

@mikegwhit
Copy link
Author

Gotcha, thanks this is helpful, is this change to reflect a more functional style of programming per chance? Do changes made into req.url reflect back onto req.query? How might a person trigger the rebuild of the req.query after a change to req.url has been made?

@dougwilson
Copy link
Contributor

Hi! So the changes you make to req.url are instantly reflected in req.query (but you have to call req.query after the change--the object you got doesn't auto-update).

@sebilasse
Copy link

@dougwilson sorry for asking here, but it is not clear to me:
If I would e.g. like to use another query-parser it is the easiest way to
"re" defineGetter(req, 'query', - right?
Or can I also do

var RQL = function (req, res, next) {
  var qStr = url.parse(req.originalUrl).search.substr(1);
  req.query = (!qStr.length) ? {} : rql.parseQuery(qStr);
  next();
};
app.use(RQL);

?

@dougwilson
Copy link
Contributor

Hi @sebilasse , you want to use the "query parser" Express application setting (http://expressjs.com/4x/api.html#app.set):

// In Express 4.x, this has to be set before you define your first route; in 5.x you can set at any time
app.set('query parser', function (str) {
  return str.length ? rql.parseQuery(str) : {};
});

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

No branches or pull requests

3 participants