티스토리 뷰

오류발생

  @Get()
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth('access-token')
  @ApiOperation({ summary: '전체 주문 조회' })
  async findAll(@Request() req) {
    const userId = req.user.id;

    return this.orderService.findAll(userId);
  }
  @Get(':id')
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth('access-token')
  @ApiOperation({ summary: '주문 조회' })
  async findOne(@Request() req, @Param('orderId', ParseIntPipe) orderId: number) {
    const userId = req.user.id;

    return this.orderService.findOne(userId, orderId);
  }

이런 평범한 API 만들고 있는데 프로젝트내에 .http 는 정상적으로 실행됐지만 swagger 를 통해 테스트했다.

{
  "message": "Validation failed (numeric string is expected)",
  "error": "Bad Request",
  "statusCode": 400
}

의 오류를 반환했다.


원인

처음엔 Bad Request 에 꽂혀 코드의 문제라고 생각하고 코드단을 살펴보았다.
controller - service 를 천천히 살펴봤지만 전혀..
그럼 jwt guard 의 문제인가? 도 아니었다.
validation failed = 유효성 검사 실패 인건데 도대체 무슨 유효성 검사에 실패한지가 이해가 안갔다.

입력하는건 orderId 값이고 그 마저도 type을 number로 지정했다.
로그도 전혀 찍히지 않음.
결국 ParseIntPipe 에서 유효성 검사에 걸렸다는 생각이 들었다.

해결방법

1. 라우팅 시스템 문제

@Get(’:id’) 의 코드를 맨 밑으로 옮기면 해결된다는 글을 봤다.
진심… 왜?… 진짜 이해가 안되고 여전히 같은 에러가 떴다.

2. 변수값 맞추기

메소드에서 :id 를 적었다면 Param 에서도 같은 변수인 ‘id’ 로 받아준다.

  @Get(':id')
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth('access-token')
  @ApiOperation({ summary: '주문 조회' })
  async findOne(@Request() req, @Param('orderId', ParseIntPipe) orderId: number) {
    const userId = req.user.id;

    return this.orderService.findOne(userId, orderId);
  }

이 코드에서

  @Get(':id')
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth('access-token')
  @ApiOperation({ summary: '주문 조회' })
  async findOne(@Request() req, @Param('id', ParseIntPipe) orderId: number) {
    const userId = req.user.id;

    return this.orderService.findOne(userId, orderId);
  }

이렇게 …. ^^…. 이게 바로 자바스크립트의 세계인가…?
이런 말도 안되는 걸 몸이 안좋은 날 잡고 있어서 짜증 폭발 ㅠㅠㅋㅋㅋㅋ