Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Handling Query Parameters | Section
Building Backend Applications with Nest.js

bookHandling Query Parameters

Swipe to show menu

Query parameters allow you to pass additional data in the URL. They are commonly used for filtering, sorting, or searching.

A query parameter is added after the ? symbol:

/users?role=admin

To access query parameters in Nest.js, use the @Query() decorator:

import { Controller, Get, Query } from '@nestjs/common';

@Controller('users')
export class UsersController {
  @Get()
  getUsers(@Query('role') role: string) {
    return `Role: ${role}`;
  }
}

  • @Query('role'): extracts the value from the URL;
  • role: contains the query parameter value.

You can also access multiple query parameters:

/users?role=admin&active=true

@Get()
getUsers(
  @Query('role') role: string,
  @Query('active') active: string
) {
  return { role, active };
}

Query parameters are optional and are often used to control how data is returned.

question mark

What decorator is used to access query parameters in Nest.js?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 8

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Section 1. Chapter 8
some-alt