개발 학습일지(TIL)

[TIL] : Nest can't resolve dependencies, 의존성 주입! @Injectable() 데코레이터 잘못 쓰면 에러가 발생한다.

Veams 2023. 3. 12.

문제상황 및 에러 메시지

 

 

[Nest] 19112  - 2023. 03. 12. 오후 9:22:49   ERROR [ExceptionHandler] Nest can't resolve dependencies of the SearcherService (?). Please make sure that the argument SearcherRepositoryRepository at index [0] is available in the SearcherModule context.

Potential solutions:
- Is SearcherModule a valid NestJS module?
- If SearcherRepositoryRepository is a provider, is it part of the current SearcherModule?
- If SearcherRepositoryRepository is exported from a separate @Module, is that module imported within SearcherModule?       
  @Module({
    imports: [ /* the Module containing SearcherRepositoryRepository */ ]
  })

 

 

의존성 주입을 다시 해보기 위하여 리팩토링 하던 도중, 에러 메시지가 생겼다.

SearcherRepositoryRepository??

모양새가 좀 웃기다..

시도1

 

https://veams.tistory.com/72

 

[TIL] : ERROR [ExceptionsHandler] No metadata for * was found. EntityMetadataNotFoundError: No metadata for * was found.

이 에러로 인해 고생하고 있었다. ERROR [ExceptionsHandler] No metadata for "SearcherRepository" was found. EntityMetadataNotFoundError: No metadata for "SearcherRepository" was found. SearcherRepository 파일을 찾을 수 없다는 에러이

veams.tistory.com

혹시 이전에 발생했던 에러와 동일한가 싶어서,

모듈에 작성한 코드를 잘 확인해보아도 해결되지 않은 것으로 보아 에러가 생긴 지점이 다른듯 했다.

 

에러메시지에 Nest can't resolve dependencies를 보아, 의존성주입하는데 뭔가 문제가 생긴것이다.

 

시도2

 

재현한 이미지, 현재는 타입스크립트가 걸러주지만, 이전에는 걸러주지 않아 파악할 수 없었다!, 아래 콘트롤러쪽에서 유사하게 발생한 이미지에서는 타입스크립트가 걸러주지 않는 것을 볼 수 있다.

이미 커스텀리포지터리를 만든 상황이다.

searcher.service.ts 쪽에 의존성 주입 코드를 작성하는 과정에서,

이전에 잘못 작성한 의존성주입 관련 코드를 깜빡하고 지우질 않아서 이런 문제가 생겼다.

 

 

        @InjectRepository(SearcherRepository)

--> 이 코드에 문제가 생긴 것이다.

위 코드를 지우니 서버가 작동하다가 멈추었다.

서버가 열리다가 만다 것이다.

 

 

위 코드를 지우니 다음과 같은 문제가 다시 나타났다.

비슷한 에러메시지이다.

 

에러메시지

[Nest] 15580  - 2023. 03. 12. 오후 9:12:28   ERROR [ExceptionHandler] Nest can't resolve dependencies of the SearcherController (?). Please make sure that the argument SearcherServiceRepository at index [0] is available in the SearcherModule context.

Potential solutions:
- Is SearcherModule a valid NestJS module?
- If SearcherServiceRepository is a provider, is it part of the current SearcherModule?
- If SearcherServiceRepository is exported from a separate @Module, is that module imported within SearcherModule?
  @Module({
    imports: [ /* the Module containing SearcherServiceRepository */ ]
  })

 

에러메시지는 좀 다르다.

SearcherServiceRepository??

 

 

 

 

    @InjectRepository(SearcherService)

이번에는 콘트롤러 쪽을 보니 잘못 쓴 위 코드가 있었다.

 

결과

@InjectRepository()를 지우면 잘 작동한다.

 

알게 된 점

의존성주입 제대로 알고 쓰자.

@InjectRepository() 데코레이터를

적절한 곳에, 적절하게 사용할 것!

잘못 쓴 것으로 이 문제가 생긴 것이다!

 

댓글