'@Get DTO with multiple parameters in NestJs

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;
}


Solution 1:[1]

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);
  }
}

Solution 2:[2]

Nest doesn't support the ability to automatically convert Get query params into an object in this way. It's expected that you would pull out the params individually by passing the name of the param to the @Param decorator.

Try changing your signature to:

login(@Param('userName') userName: string, @Param('password') password: string)

If you want to receive an object instead consider switching to using Post and passing the object in the request body (which makes more sense to me for a login action anyways).

Solution 3:[3]

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 !

Solution 4:[4]

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';
  }

Solution 5:[5]

 @Get('/login/:email/:password')
 @ApiParam({name:'email',type:'string'})
 @ApiParam({name:'password',type:'string'})
 login(@Param() params: string[]) {
   console.log(params)
   return 'OK'
}

Output

{email:<input email >,password:<input password>}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 W.Perrin
Solution 2 Jesse Carter
Solution 3 Thiago Dantas
Solution 4 Gimnath
Solution 5 Tejesh Teju