0
0
NestJSframework~10 mins

Query parameters in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to extract query parameters in a NestJS controller method.

NestJS
import { Controller, Get, [1] } from '@nestjs/common';

@Controller('items')
export class ItemsController {
  @Get()
  findAll(@[1]() query: any) {
    return query;
  }
}
Drag options to blanks, or click blank then click option'
AQuery
BBody
CParam
DHeaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Body() instead of @Query() to get query parameters.
Using @Param() which extracts route parameters, not query parameters.
2fill in blank
medium

Complete the code to type the query parameter object in a NestJS controller.

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

@Controller('search')
export class SearchController {
  @Get()
  search(@Query() query: [1]) {
    return query;
  }
}
Drag options to blanks, or click blank then click option'
Anumber
Bstring
Cany
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using string which only allows a single string, not an object.
Using number or boolean which are too restrictive.
3fill in blank
hard

Fix the error in the code to correctly extract a specific query parameter named 'page'.

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

@Controller('posts')
export class PostsController {
  @Get()
  getPosts(@Query('[1]') page: string) {
    return `Page number is ${page}`;
  }
}
Drag options to blanks, or click blank then click option'
Apage
BPage
Cpages
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect casing like 'Page' instead of 'page'.
Using plural or different names like 'pages' or 'number'.
4fill in blank
hard

Fill both blanks to destructure 'limit' and 'offset' query parameters with default values.

NestJS
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] };
  }
}
Drag options to blanks, or click blank then click option'
Alimit
Boffset
Cpage
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names like 'page' or 'size'.
Not providing default values causing undefined errors.
5fill in blank
hard

Fill all three blanks to validate and transform a query parameter 'count' to a number using a pipe.

NestJS
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}`;
  }
}
Drag options to blanks, or click blank then click option'
AParseIntPipe
Bcount
CValidationPipe
DParseBoolPipe
Attempts:
3 left
💡 Hint
Common Mistakes
Using ValidationPipe which does not convert types automatically.
Using ParseBoolPipe which is for boolean values.
Mismatching parameter name.