스파르타 내배캠 _과제, 프로젝트, 특강
타입에러 해결 - string을 숫자로 변환하려는데 객체가 되어서 NaN되는 현상
momomoo
2024. 7. 12. 13:48
@Patch(':listId')
async update(@Param('listId', ParseIntPipe) listId: number, @Body() updateListDto: UpdateListDto) {
const data = await this.listsService.update(listId, updateListDto);
return {
statusCode: HttpStatus.OK,
message: '리스트 수정에 성공했습니다.',
data,
};
}
ParseIntPipe로 해결
이건 원래 코드 콘솔로 찍어보는데
@Patch(':listId')
async update(@Param('listId') listId: string, @Body() updateListDto: UpdateListDto) {
console.log('listId:', listId); // listId가 문자열인지 확인
console.log('typeof listId:', typeof listId); // listId의 타입을 확인
console.log('updateListDto:', updateListDto); // updateListDto 내용 확인
console.log('updateListId:', +listId); // updateListDto 내용 확인
const data = await this.listsService.update(+listId, updateListDto);
return {
statusCode: HttpStatus.OK,
message: '리스트 수정에 성공했습니다.',
data,
};
}
로직은 도는데 처음은 string이 나오고
요청값은 잘 들어오는데
+listId에서 객체로 변해서 id를 찾지 못하는거였다