From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Fetching queries

Fetching queries

In NestJS, the query decorator is used to extract query parameters from the incoming HTTP request. It is commonly used with the GET decorator to handle GET requests and retrieve data based on query parameters. It works just like the param decorator, but instead of fetching the URL params, it fetches the URL query parameters. Query parameters are typically appended to the URL after a question mark and separated by an ampersand sign. For example, if we have a URL like this, the name and ID are considered as query parameters. Also giving the name and ID inside the query decorators argument would extract its given values. Let's try a practical example. Inside the controller file, I'll give the get handler and give the method as fetch query. Then I'll give the query decorator inside the method with the argument as name, which we want to fetch. the property in which we want to fetch the query parameter value. Then I'll simply return a string message, which displays the query parameter value…

Contents