Today I learned
photospot을 모두 가져오는 작업을 진행하다가 문제가 발생했다
deletedAt이 null인 데이터만 가져오고 싶었다
async getAllPhotospot() {
return this.photospotRepository.find({where: {deletedAt: null})
// error
'{ deletedAt: null; }' 형식은 'FindOptionsWhere<Photospot> | FindOptionsWhere<Photospot>[] | undefined' 형식에 할당할 수 없습니다.
'deletedAt' 속성의 형식이 호환되지 않습니다.
'null' 형식은 'Date | FindOperator<Date> | undefined' 형식에 할당할 수 없습니다.ts(2322)
타입스크립트가 에러를 보여준다 null을 사용할 수 없다는 거다
해결한 방법은 간단하다
async getAllPhotospot() {
return this.photospotRepository.find()
그냥 find만 사용해도 DeleteDateColumn이 null인 데이터만 가져온다
하지만 아직 이해가 안간다 소프트삭제를 연습할 때 where이 잘 사용이 되었는데 왜 이번에는
문제가 발생하는걸까?
tsconfig에 설정되어있는 엄격한 널 체크 때문에 그렇다
"strictNullChecks": true,
photospot 수정을 작업하고 예외처리를 진행하다가 문제가 발생했다
async getPhotospot({ collectionId, photospotId }: { collectionId: number; photospotId: number }): Promise<Photospot | null> {
const photospot = await this.photospotRepository.findOne({ where: { collectionId, id: photospotId } });
if (_.isNil(photospot)) {
throw new NotFoundException('해당 포토스팟을 찾을 수 없습니다.');
}
return photospot;
}
async modifyPhotospot(modifyPhotospotDto: ModifyPhotospotDto, param: { collectionId: number; photospotId: number }): Promise<void> {
const { title, description, image }: ModifyPhotospotDto = modifyPhotospotDto;
const { collectionId, photospotId }: { collectionId: number; photospotId: number } = param;
let updateData;
await this.getPhotospot({ collectionId, photospotId });
if (_.isNil(image)) {
updateData = { title, description };
} else {
const imagePath = await this.s3Service.putObject(image);
updateData = { title, description, imagePath };
}
this.photospotRepository.update({ id: photospotId }, updateData);
}
이렇게 getPhotospot을 실행해서 없는 경우 예외처리를 진행한다
그냥 photospot을 조회할 때 없는 photospot을 조회하면 null이 찍히면서 예외처리를 진행하는데
수정을 할 때 조회를 하면 서버가 다운되어버리는 문제다
문제는 비동기 처리였다
@Put('/:collectionId/photospots/:photospotId')
@FormDataRequest()
modifyPhotospot(
@Body() modifyPhotospot: ModifyPhotospotDto,
@Param() param: { collectionId: number; photospotId: number }
): void {
this.photospotService.modifyPhotospot(modifyPhotospot, param)
}
처음에 put은 반환되는 값이 없다고 생각해서 비동기 처리를 하지 않아도 된다고 생각하고 작업을 진행했다 하지만 여기서 문제가 발생하는거 였다 이미 요청이 끝나버리고 예외가 발생해서 요청에 대한 에러가 반환되는게 아니라 그냥 에러가 발생해서 터지는 문제였다
@Put('/:collectionId/photospots/:photospotId')
@FormDataRequest()
async modifyPhotospot(
@Body() modifyPhotospot: ModifyPhotospotDto,
@Param() param: { collectionId: number; photospotId: number }
): void {
await this.photospotService.modifyPhotospot(modifyPhotospot, param)
}
이런식으로 photospotService.modifyPhotospot이 끝나는것을 기다리면 정상적으로 예외처리가
발생한다
'과거공부모음' 카테고리의 다른 글
20230307 TIL - github에 민감한 파일 올렸다 (0) | 2023.03.07 |
---|---|
20230306 TIL - S3 파일 업로드 문제 (0) | 2023.03.06 |
20230228 TIL - nestjs 유닛테스트 (0) | 2023.03.01 |
20230227 TIL - 최종프로젝트 (찰칵), nestjs-form-data (0) | 2023.02.27 |
20230222 TIL - nestjs AWS S3 file upload (2) | 2023.02.22 |