Johnny
Johnny

Reputation: 291

Nestjs: Cant implement mutiple GET methods in same controller

I have some weird issue here. I have 2 get method which is one get staff list by companyId & get staff details by staffId. Second method getStaffById doesn't work. If I comment out the first get method getStaffByCompanyId, second working as expected. Looks like first get method blocking second method. Did I missed anything here?

@Controller('staff')
@ApiTags('Staff')
export class StaffController {
  constructor(private staffService: StaffService) {}

  @Get(':companyId')
  @ApiResponse({
    status: HttpStatus.OK,
    description: 'Return staffs by id',
  })
  @ApiResponse({
    status: HttpStatus.UNAUTHORIZED,
    description: 'Unauthorized',
    type: Error,
  })
  @ApiResponse({
    status: HttpStatus.UNPROCESSABLE_ENTITY,
    description: 'Get particular staff details',
    type: Error,
  })
  @ApiResponse({
    status: HttpStatus.INTERNAL_SERVER_ERROR,
    description: 'Internal server error',
    type: Error,
  })
  @ApiResponse({
    status: HttpStatus.BAD_GATEWAY,
    description: 'Internal communication error',
    type: Error,
  })
  @ApiOperation({
    operationId: 'getStaffByCompanyId',
    summary: 'Get staff list that attach to the company',
  })
  async getStaffByCompanyId(@Param('companyId') companyId: string): Promise<IStaff[]> {
    return await this.staffService.getStaffByCompanyId(companyId);
  }

  @Get(':staffId')
  @ApiResponse({
    status: HttpStatus.OK,
    description: 'Return staff by id',
  })
  @ApiResponse({
    status: HttpStatus.UNAUTHORIZED,
    description: 'Unauthorized',
    type: Error,
  })
  @ApiResponse({
    status: HttpStatus.UNPROCESSABLE_ENTITY,
    description: 'Get particular staff details',
    type: Error,
  })
  @ApiResponse({
    status: HttpStatus.INTERNAL_SERVER_ERROR,
    description: 'Internal server error',
    type: Error,
  })
  @ApiResponse({
    status: HttpStatus.BAD_GATEWAY,
    description: 'Internal communication error',
    type: Error,
  })
  @ApiOperation({
    operationId: 'getStaffById',
    summary: 'Get staff profile details',
  })
  async getStaffById(@Param('staffId') staffId: string): Promise<IStaff> {
    return await this.staffService.getStaffById(staffId);
  }
}


Upvotes: 1

Views: 2685

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70111

Express (what Nest is most likely using under the hood for you) cannot differentiate between

app.get('/:companyId', (req, res, next) => {})

and

app.get('/:staffId', (req, res, next) => {})

Because all it can see is that there will be a request, with some sort of generic parameter coming in as a string, so while you may know that a companyId starts with a C and a staffId starts with an S, there's no way to tell that to Express, and because of this, there's no way to tell Nest to tell Express either. The best thing you can do is add a prefix like /comapny/:companyId and /staff/:staffId to your routes, to make sure they stay separated.

Upvotes: 6

Related Questions