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

bookHandling Query Parameters

Svep för att visa menyn

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?

Vänligen välj det korrekta svaret

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 8

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Avsnitt 1. Kapitel 8
some-alt