I'm trying to create a controller action in NestJS accessible via GET HTTP request which receives two params but they are undefined for some reason.
How to fix it?
@Get('/login')
login(@Param() params: LoginUserDto) {
console.log(params)
return 'OK'
}
import { ApiModelProperty } from '@nestjs/swagger';
export class LoginUserDto {
@ApiModelProperty()
readonly userName: string;
@ApiModelProperty()
readonly password: string;
}
In Browser
localhost:3001/Products/v1/user2
Controller like this:
@Controller('Products')
export class CrashesController {
constructor(private readonly crashesService: CrashesService) { }
@Get('/:version/:user')
async findVersionUser(@Param('version') version: string, @Param('user') user: string): Promise<Crash[]> {
return this.crashesService.findVersionUser(version, user);
}
}
Let's say you need to pass a one required parameter named id
you can send it through header params
, and your optional parameters can be sent via query params
;
@Get('/:id')
findAll(
@Param('id') patientId: string,
@Query() filter: string,
): string {
console.log(id);
console.log(filter);
return 'Get all samples';
}
Right now i am using nestJs on 7.0.0 and if you do this:
@Get('/paramsTest3/:number/:name/:age')
getIdTest3(@Param() params:number): string{
console.log(params);
return this.appService.getMultipleParams(params);
}
the console.log(params) result will be(the values are only examples):
{ number:11, name: thiago, age: 23 }
i hope that after all that time i've been helpful to you in some way !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With