Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 월간cs
- npm
- 와글와글
- TypeScript 타입챌린지
- 스터디
- 이펙티브 타입스크립트
- 백엔드
- 타입 챌린지
- mysql
- 굿바이 2024년
- mysql boolean
- 타입챌린지
- 회고록
- TypeScript
- typescript type challenge
- 타입스크립트
- network
- HTTP
- typeorm
- microsoft azure openai
- configservice
- 회고
- configmodule
- 2024년 회고록
- node.js
- Type Challenge
- nestjs
- type-safe configservice
- 해커톤
- 코딩테스트
Archives
- Today
- Total
iamkanguk.dev
[NestJS - 트러블슈팅] Cannot read properties of undefined (reading 'joinColumns') 본문
Framework/NestJS
[NestJS - 트러블슈팅] Cannot read properties of undefined (reading 'joinColumns')
iamkanguk 2023. 11. 23. 19:28최근에 Hard-Delete에서 Soft-Delete로 변경하면서 repository method와 entity 쪽에 전반적으로 변경이 일어났는데..
오늘 개발한 API들을 점검하면서 글 제목과 같이 에러가 발생했다. JoinColumns는 FK를 연결할 때 쓰는건데 아무리 봐도 잘못된 부분이 없는데 어디가 문제인지 도저히 모르겠는 것이다.
위의 에러는 Soft-Delete를 하는 과정에서 발생한 이슈이다.
필자가 참여하고 있는 프로젝트에서는 Schedule과 ScheduleArea 엔티티가 각각 있다. 기존 고드를 제공해보겠다.
// ScheduleEntity
@OneToMany(
() => ScheduleAreaEntity,
(scheduleArea) => scheduleArea.scheduleId,
{
cascade: true,
},
)
scheduleAreas: ScheduleAreaEntity[];
// ScheduleAreaEntity
@ManyToOne(() => ScheduleEntity, (schedule) => schedule.scheduleAreas, {
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
})
@JoinColumn({ name: 'scheduleId', referencedColumnName: 'id' })
schedule: ScheduleEntity;
위의 코드는 Entity 끼리 연관관계를 맺어주는 부분이라고 보면 된다. 딱 보면 어디가 문제가 있어보이는가? 왜 나만 안보일까...?
문제점을 찾음!
문제점은 바로 ScheduleEntity 부분쪽에 (scheduleArea) => scheduleArea.scheduleId 부분이다.
왜냐면 나는 ScheduleAreaEntity 쪽에는 schedule 이라고 FK 연결 설정을 해놨는데, 필자는 JoinColumn의 name 부분에 scheduleId라고 설정을 해놔서 scheduleArea.scheduleId로 접근을 한 것 같다.
// ScheduleEntity
@OneToMany(
() => ScheduleAreaEntity,
(scheduleArea) => scheduleArea.schedule,
{
cascade: true,
},
)
scheduleAreas: ScheduleAreaEntity[];
// ScheduleAreaEntity
@ManyToOne(() => ScheduleEntity, (schedule) => schedule.scheduleAreas, {
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
})
@JoinColumn({ name: 'scheduleId', referencedColumnName: 'id' })
schedule: ScheduleEntity;
그래서 위와 같이 코드를 수정하면 정상적으로 작동을 하는 것을 확인할 수 있다!
다시 한번 꼼꼼하게 살펴보자!
'Framework > NestJS' 카테고리의 다른 글
[NestJS] Class-Validator 에러 메세지 커스텀하기! (0) | 2023.12.16 |
---|---|
[NestJS] 자네 혹시 helmet이 뭔지 알고 쓰나? (0) | 2023.11.29 |
[NestJS] Class-transformer의 expose와 exclude를 사용할 때 캐싱에서 발생한 이슈 + Lifecycle 분석 (1) | 2023.11.15 |
[NestJS] ResponseEntity와 정적 팩토리 메서드 (1) | 2023.11.14 |
[NestJS] Nestia swagger 초기 설정에서 발생하는 문제 (2) | 2023.11.13 |