Typeorm 에러 해결기

영슈·2023년 9월 13일
0

인턴십-NestStreaming

목록 보기
1/2

깃허브 주소

Nest-Streaming

진행 일지

1.[nest-streaming:feat] 음반 기능 CRUD 추가
참조 링크 : https://github.com/node9137/nest-streaming/pull/6
2.[nest-streaming:fix] soundtrack Repository 부분 Error 해결

기록할 점

=> CustomRepository Decorator 로 , Repository 화 해서 , DB Logic을 수행하려 했으나,
계속 Entity 의 Metadata 가 Not Found 되는 오류가 발생해서 삭제하고 , 다시 만들고 db 날리고 다시해도 에러가 떴는데 ,
nestjs/typeorm 에서 제공하는 TypeORM 을 통해서 , 그냥 DI 를 하니 , 작동함

  • stackoverflow 를 보면 , 전부 여기 entities 에서 해당 Entity 를 못찾아서 문제가 발생한다고 하는데,
    All classes found using provided glob pattern "dist/*/.entity.js" : "dist/src/models/common/common-base.entity.js,dist/src/models/soundtrack.entity.js,dist/src/models/user.entity.js"
    이렇게 Entity 를 찾았다는 Log 가 뜬다.
soundtrack.module.ts

CustomTypeOrmModule.forCustomRepository([SoundtrackRepository])

=================================================>

{
provide: 'SOUNDTRACK_REPOSITORY',

useFactory: (dataSource: DataSource) => dataSource.getRepository(SoundtrackEntity),

inject: [DataSource],

},
soundtrack.service.ts

private readonly soundtrackRepository : SoundtrackRepository){}

=================================================>
@Inject('SOUNDTRACK_REPOSITORY')
private readonly soundtrackRepository : SoundtrackRepository){}
  • 당연히 , Inject 를 해야하는게 아니냐고 말할 수 있으나 , 위와 동일하게 수행한 user.module.ts 에서는
    정상적으로 작동을 한다.

위와 같이 해도 동작을 한다.

혹시나 , Custom Repository 구성 할 때 ,
Entity 를 import 하고 , 동작하는 걸 확인해도 Metadata was not found 가 뜬다면 ,
권장 방법대로 TypeormModule 에서 직접 받고 주입 하자.

사담

  • 특정 에러가 발생하면 , Docs 를 따라하자.
Writed By Obisidan
profile
https://youngsu5582.life/ 로 블로그 이동중입니다~

0개의 댓글