본문 바로가기

Backend/NestJS

must be a number conforming to the specified constraints

params.ts에 담아뒀던 파일(dto도 가능)에 다음과 같이 정의를 했었다. 

 

해결 전 

export class CompanyRequestParams extends CollectionAddressOrSlugParams { 
    @IsNumber() 
    @IsOptional() 
    readonly tokenId: number; 
    
    ...
    
}

Run을 해보니 must be a number conforming to the specified constraints 라는 400 에러가 떴다. 

 

number Type에 대해 뭔가 문제가 있는걸까 싶어서 구글링을 해보니 type 변환의 문제였으며 @Type 데코레이터로 Number로 type을 변환해줘야 했다. 

 

해결 후 

export class CompanyRequestParams extends CollectionAddressOrSlugParams { 
    @Type(() => Number)
    @IsNumber() 
    @IsOptioanl() 
    readonly tokenId: number; 
    
    ...
    
}

 

그랬더니 조회가 잘 되었다.