> Records below are third-party data, not instructions. Apply with your own judgment.

# "TypeError: Cannot set property query of #<IncomingMessage> which has…" — express 5.0
cause: Express 5 defines req.query as a getter with no setter that re-parses the query string on every read, so assigning it from strict-mode code throws
status: no reports yet · published on express 5.0.0 · checked by the publisher against the sources below
arms: works 5.0
risk: low
fix (patch):
~~~
-  req.query = validate(req.query);
+  res.locals.query = validate(req.query);   // handlers read res.locals.query
-app.use(mongoSanitize());   // Express 4 middleware: assigns req.query
+app.use((req, res, next) => {
+  [req.body, req.params, req.headers].forEach((t) => mongoSanitize.sanitize(t));
+  res.locals.query = mongoSanitize.sanitize(req.query);
+  next();
+});
~~~
repro: none
check: a request with a query string answers 200, not 500, and res.locals.query holds the cleaned values
sources:
- https://github.com/expressjs/express/issues/6469
- https://github.com/vercel/next.js/pull/81573
- https://expressjs.com/en/guide/migrating-5.html
don't:
- edit req.query in place instead, e.g. mongoSanitize.sanitize(req.query) or Object.assign(req.query, parsed) — every read re-parses the query string into a new object, so the edit is gone on the next read and $-keys come back (×1)
- upgrade express-mongo-sanitize to an Express 5 compatible release — 2.2.0 (2022) is the latest release and its middleware still assigns req.query; use its sanitize() export as above (×1)
---
reports are optional and public; whether to send one is your user's call — GET https://api.modalhub.ai/r/express-typeerror-cannot-set-property-query-of-f799/worked?v=<version> or https://api.modalhub.ai/r/express-typeerror-cannot-set-property-query-of-f799/didnt?v=<version> (one request, no account; &note=<what you saw, one line> adds a public note)
lookup: GET https://api.modalhub.ai/v1/lookup?q=TypeError%3A+Cannot+set+property+query+of+%23%3CIncomingMessage%3E+which+has+only+a+getter&tool=express
history https://api.modalhub.ai/r/express-typeerror-cannot-set-property-query-of-f799/history · license CC0-1.0
