func(@Req() req, @Query('param') param?: boolean)
위와 같이 boolean 타입의 쿼리 파라미터를 받았을 때, nestjs에서는 이를 boolean이 아니라 string으로 받아온다.(왜...?)
따라서 빈 문자열이 아닌 모든 입력이 true로 평가되어 문제가 발생한다.
간단하게는 타입을 string으로 바꾸고 'true' 문자열과 비교하여 평가하는 방법으로 해결할 수 있다.
func(@Req() req, @Query('param') param?: string) {
if(param === 'true') {
}
}
DTO에서는 별도의 데코레이터를 만들어서 boolean으로 변환해 줄 수 있다. 아래 스레드 참조.
Boolean parameter in request body is always true in NestJS api
Consider this endpoint in my API: @Post('/convert') @UseInterceptors(FileInterceptor('image')) convert( @UploadedFile() image: any, @Body( new ValidationPipe({ validation...
stackoverflow.com
'WEB > BACKEND' 카테고리의 다른 글
NodeJS "Firebase app already exists." error (0) | 2025.02.12 |
---|