Complete the code to extract query parameters in a NestJS controller method.
import { Controller, Get, [1] } from '@nestjs/common'; @Controller('items') export class ItemsController { @Get() findAll(@[1]() query: any) { return query; } }
The @Query() decorator extracts query parameters from the URL in NestJS.
Complete the code to type the query parameter object in a NestJS controller.
import { Controller, Get, Query } from '@nestjs/common'; @Controller('search') export class SearchController { @Get() search(@Query() query: [1]) { return query; } }
string which only allows a single string, not an object.number or boolean which are too restrictive.Query parameters can be any shape, so any is often used for flexibility.
Fix the error in the code to correctly extract a specific query parameter named 'page'.
import { Controller, Get, Query } from '@nestjs/common'; @Controller('posts') export class PostsController { @Get() getPosts(@Query('[1]') page: string) { return `Page number is ${page}`; } }
The string inside @Query() must exactly match the query parameter name, which is 'page'.
Fill both blanks to destructure 'limit' and 'offset' query parameters with default values.
import { Controller, Get, Query } from '@nestjs/common'; @Controller('data') export class DataController { @Get() fetchData(@Query() { [1] = 10, [2] = 0 }: any) { return { limit: [1], offset: [2] }; } }
Destructuring the query object with default values for limit and offset allows setting defaults if parameters are missing.
Fill all three blanks to validate and transform a query parameter 'count' to a number using a pipe.
import { Controller, Get, Query, [1] } from '@nestjs/common'; @Controller('stats') export class StatsController { @Get() getStats(@Query('[2]', new [3]()) count: number) { return `Count is ${count}`; } }
ValidationPipe which does not convert types automatically.ParseBoolPipe which is for boolean values.The ParseIntPipe converts the 'count' query parameter from string to number and validates it.