Consider the following NestJS controller method that reads query parameters:
@Get('items')
getItems(@Query('page') page: string, @Query('limit') limit: string) {
return `Page: ${page}, Limit: ${limit}`;
}What will be the response body when a client requests /items?page=3&limit=10?
@Get('items') getItems(@Query('page') page: string, @Query('limit') limit: string) { return `Page: ${page}, Limit: ${limit}`; }
Query parameters are passed as strings from the URL and injected by NestJS into the method parameters.
The @Query('param') decorator extracts the query parameter as a string. Since the URL has page=3 and limit=10, these values are passed as strings to the method, resulting in the returned string showing those values.
In NestJS, you want to get all query parameters as a single object inside a controller method. Which code snippet correctly does this?
Use @Query() without parameters to get all query params as an object.
The @Query() decorator without arguments injects all query parameters as an object. Option D correctly uses this syntax. Option D tries to get a parameter named 'query' which is incorrect. Option D and D are invalid syntax.
Given this NestJS controller method:
@Get('search')
search(@Query('page') page: number = 1) {
return page;
}What will be the returned value if the client calls /search?page=5?
@Get('search') search(@Query('page') page: number = 1) { return page; }
Query parameters are strings by default. TypeScript types do not convert them automatically.
The @Query('page') page: number = 1 declares page as a number in TypeScript, but NestJS injects the query parameter as a string ('5'). TypeScript types do not affect runtime behavior or perform automatic conversion. Thus, page is the string "5", and it is returned as such.
Consider these NestJS controller methods. Which one will cause a runtime error when called with /data?filter=active?
Check which string method does not exist or is invalid.
Options A, B, and D call valid string methods. Option C calls toFixed, which is a method for numbers, not strings. Since filter is a string, calling toFixed causes a runtime error.
Given this controller method:
@Get('list')
list(@Query('page') page: number = 1) {
return page;
}If the client calls /list without any query parameters, what will be the returned value?
@Get('list') list(@Query('page') page: number = 1) { return page; }
Consider how NestJS injects query parameters and how default values in TypeScript work with decorators.
The default value = 1 in the method signature does not apply because NestJS injects undefined when the query parameter is missing. The decorator parameter overrides the default, so page is undefined. To have a default, you must handle it inside the method or use pipes.