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

add query setter #332

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

add query setter #332

wants to merge 1 commit into from

Conversation

fawzy0z
Copy link

@fawzy0z fawzy0z commented Jan 1, 2025

Hello,

I’ve been working on a validation middleware to sanitize query parameters. My specific use case involved normalizing an email query parameter (e.g., trimming spaces and converting it to lowercase) so that the sanitized value could be used consistently in subsequent middleware or route handlers.

During implementation, I encountered the following errors when attempting to set a new value for the query property:

  • TypeError: Cannot set property query of # which has only a getter.
  • TypeError: Cannot set property query_parameters of # which has only a getter.

Solution

I’ve identified the cause and resolved the issue. My approach ensures the query parameters are sanitized without conflicting with the Request object’s read-only properties. This solution should help anyone facing a similar problem.

Looking forward to your feedback!

Thank you.

@zenozaga
Copy link

You can overwrite the request object so that query returns a custom value.

This was my solution:

 Object.defineProperty(req, "query", {
    get() {
      return parse(this.path_query, options);
    },
  });

Also, for global use, you can create a middleware:

const customQueryParseMiddleware:MiddlewareHandler = (req, res, next) => {
   Object.defineProperty(req, "query", {
      get() {
        return parse(this.path_query, options);
      },
    });
}


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

Successfully merging this pull request may close these issues.

2 participants