Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the DefaultValuePipe from NestJS.
NestJS
import { [1] } from '@nestjs/common';
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the wrong pipe like ValidationPipe or ParseIntPipe.
Forgetting to import the pipe.
✗ Incorrect
The DefaultValuePipe is imported from '@nestjs/common' to provide default values for parameters.
2fill in blank
mediumComplete the code to apply DefaultValuePipe with a default value of 'guest' to the 'username' query parameter.
NestJS
@Query('username', new [1]('guest')) username: string
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ParseIntPipe or ValidationPipe instead of DefaultValuePipe.
Not passing the default value as a constructor argument.
✗ Incorrect
DefaultValuePipe is used to provide a default value when the query parameter is missing.
3fill in blank
hardFix the error in the code to correctly use DefaultValuePipe with a default value of 10 for the 'limit' query parameter.
NestJS
@Query('limit', new [1](10)) limit: number
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pipes that do not accept default values like ParseBoolPipe.
Not wrapping the pipe with 'new'.
✗ Incorrect
DefaultValuePipe accepts a default value and sets it if the query parameter is missing.
4fill in blank
hardFill both blanks to create a controller method that uses DefaultValuePipe to set a default page number of 1 and a default page size of 20.
NestJS
getItems(@Query('page', new [1](1)) page: number, @Query('size', new [2](20)) size: number) { return { page, size }; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing different pipes for the two parameters.
Using pipes that do not accept default values.
✗ Incorrect
DefaultValuePipe is used for both 'page' and 'size' to provide default values when parameters are missing.
5fill in blank
hardFill all three blanks to create a controller method that uses DefaultValuePipe and ParseIntPipe to set default values and parse integers for 'page', 'size', and 'sortOrder' query parameters.
NestJS
getData(@Query('page', new [1](1), new [2]()) page: number, @Query('size', new [3](10), new ParseIntPipe()) size: number, @Query('sortOrder', new DefaultValuePipe('asc')) sortOrder: string) { return { page, size, sortOrder }; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ParseIntPipe before DefaultValuePipe.
Not using DefaultValuePipe for default values.
Using ValidationPipe instead of ParseIntPipe.
✗ Incorrect
DefaultValuePipe sets default values; ParseIntPipe parses string to integer. The method uses both pipes for 'page' and 'size'.