16

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

6 Answers 6

28

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

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).

4

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 }
2
  • 2
    Would there be a way to make one of these parameters optional? For instance: @Get('/paramsTest3/:number/:name/:age?') Oct 11, 2020 at 9:25
  • 1
    @MichaelLRhyndress I would use a JSON body if you want to do this Dec 10, 2020 at 13:39
2

Let's say you need to pass 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';
}
1
@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>}
1

You can get multiple params and map them to your dto in this way:

    @Get('/login')
    login(@Param() { userName, password }: LoginUserDto) {
    console.log({ userName});
    console.log({ password });
    return 'OK'
    }

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.