[ ] Regression
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
It's only possible to use parameters if they're route parameters.
For example, to access name and age, a route must be set up as:
:name/:age
It should be possible to use url parameters as well:
E.g. http://localhost:3000/cats?name=gary&age=1.57
@Get('cats')
async getByNameAndAge(@Param('name') name: string, @Param('age'): number): Promise<object> {
return Promise.resolve({"name": name, "age": age, "success": true})
}
Url parameters being parsed in any order is better for long lists of parameters and the potential for optional parameters compared to positional route parameters.
Nest version: 4.6.6
@natejgardner you can use @Query('name') or @Query('age') as well.
@chanlito +1 , exactly
@Query() is what's you are looking for.
And it also accepts an optional query string name.
Query is imported from @nestjs/common
I'll provide an example in the docs.
Is there any way to have different controller methods with a hardcoded query parameter value? Something like:
@Get("/aggregate/sum?field=myField")
or do we have to check the field name using a controller method like this:
@Get("/aggregate/sum")
async findAggregateBySum(@Query("field")field:string){
if(field==="myField"){
//etc
}
}
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
I'll provide an example in the docs.