Query parameters are key-value pairs added to the end of a URL after a question mark (?). They are used to send extra data to the server, like filters or options.
Click to reveal answer
beginner
How do you access query parameters in a NestJS controller?
You use the @Query() decorator in a controller method to get query parameters from the request URL.
Click to reveal answer
beginner
Example: How to get a single query parameter named 'page' in NestJS?
Use @Query('page') page: string in the controller method parameters to get the 'page' query parameter value.
Click to reveal answer
intermediate
What type of data does @Query() return if no parameter name is given?
@Query() returns an object containing all query parameters as key-value pairs.
Click to reveal answer
intermediate
Why is it important to validate query parameters in NestJS?
Validating query parameters helps prevent errors and security issues by ensuring the data is the right type and format before using it.
Click to reveal answer
In NestJS, which decorator is used to access query parameters?
A@Body()
B@Param()
C@Headers()
D@Query()
✗ Incorrect
@Query() is the correct decorator to access query parameters in NestJS controllers.
What does @Query('id') return in a NestJS controller method?
AThe URL path parameter 'id'
BAll query parameters as an object
CThe value of the 'id' query parameter
DThe request body
✗ Incorrect
@Query('id') extracts the value of the 'id' query parameter from the URL.
If a URL is /users?sort=asc&page=2, what does @Query() return?
A{ sort: 'asc', page: '2' }
B['sort', 'asc', 'page', '2']
Cnull
DOnly the first query parameter
✗ Incorrect
@Query() returns an object with all query parameters as key-value pairs.
Why should query parameters be validated in NestJS?
ATo avoid errors and security risks
BTo speed up the server
CTo change the URL
DTo encrypt data automatically
✗ Incorrect
Validation ensures query parameters are safe and correct before use.
Which HTTP method commonly uses query parameters?
APOST
BGET
CPUT
DDELETE
✗ Incorrect
GET requests often use query parameters to filter or sort data.
Explain how to retrieve and use query parameters in a NestJS controller.
Think about how the URL query string maps to controller method parameters.
You got /4 concepts.
Why is validating query parameters important in NestJS applications?
Consider what could happen if invalid data is used directly.
You got /4 concepts.
Practice
(1/5)
1. What is the purpose of using @Query() decorator in a NestJS controller?
easy
A. To inject a service into the controller
B. To define a new route path
C. To handle POST request bodies
D. To access query parameters sent in the URL
Solution
Step 1: Understand the role of @Query()
The @Query() decorator extracts query parameters from the URL in a GET request.
Step 2: Compare with other decorators
Other decorators like @Body() handle POST bodies, and @Param() handles route params, so they don't fit here.
Final Answer:
To access query parameters sent in the URL -> Option D
B. The @Query decorator argument should be a string: @Query('search')
C. The parameter name cannot be searchTerm
D. The method should use @Param('search') instead
Solution
Step 1: Check @Query decorator syntax
The argument to @Query() must be a string literal naming the query parameter, e.g., @Query('search').
Step 2: Identify the error in the code
The code uses @Query(search) without quotes, which is invalid syntax and causes an error.
Final Answer:
The @Query decorator argument should be a string: @Query('search') -> Option B
Quick Check:
Decorator argument must be string literal = D [OK]
Hint: Always quote query param name in @Query('name') [OK]
Common Mistakes:
Forgetting quotes around query param name
Using @Param() for query params
Thinking async keyword is disallowed
5. You want to create a NestJS GET endpoint that accepts optional query parameters sort and filter. If filter is missing, it should default to "all". Which code snippet correctly implements this behavior?