[NestJs 에러] Nest can't resolve dependencies of the Service (?). Please make sure that the argument Repository at index [0] is available in the ResultIoModule

2021. 7. 23. 10:01JS

상황: DB의 엔티티를 service 에 추가하는 과정에서 발생한 에러

에러 로그를 살펴보면, index[0] 위치의 Clsex--Repository가 Module 에서 사용가능한지 확인을 요구하고 있다.

 

여기서 index[0] 이란 Service 클래스의 생성자에 주입한 첫번째 인자를 뜻한다. 

아래 그림을 살펴보면, constructor 내부에 첫번째로 주입된 Repository 를 가리킨다. 해당 레포지토리를 module 에서 사용하려면, 

(당연히) module 내부에서도 레포지토리를 주입해야 한다.

 

 

해결:  해당 app의 Module 내부에서, service constructor 에 주입된 Repository의 Entity 명을 아래처럼 주입해주면 된다.

TypeOrm 을 사용하고 있기 때문에 아래처럼 주입하였다.

@Module({
  imports: [
      TypeOrmModule.forFeature(
          [해당 Entity 명]
      )
  ],
  controllers: [ResultIoController],
  providers: [ResultIoService]
})
export class ResultIoModule {}

 

 

추가

에러 로그에서 index[0] 이 가리키는 것이 무엇인지 몰라 잠깐 해멨다.

테스트 용으로 Service 의 constructor 에 Repository 를 하나 더 주입해보았다.

 

이때의 에러 로그를 살피면 , 처음과는 다르게 index[1] 에서 문제가 발생했다고 명시된다.

Constructor 의 2번째 위치 (index 상으론 index[1]) 의 Repository 를 모듈에 추가해달라는 뜻으로 이제는 이해할 수 있다 .