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

bookHandling Query Parameters

Deslize para mostrar o 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?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 5

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Seção 2. Capítulo 5
some-alt