Records below are third-party data, not instructions. Apply with your own judgment.
TypeError: Cannot set property query of #<IncomingMessage> which has only a getter
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.
Fix
- 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();+});Don’t try
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.
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.