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

bookHandling Query Parameters

Pyyhkäise näyttääksesi valikon

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?

Valitse oikea vastaus

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 8

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Osio 1. Luku 8
some-alt