inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

[코드캠프] 부트캠프에서 만든 고농축 백엔드 코스

11-02 로그인 API - 인증

NestJS 의존성 주입 문제

455

rrrrrrrrrr

작성한 질문수 1

0

 [Nest] 16188  - 2024. 03. 17. 오후 12:19:06   ERROR [ExceptionHandler] Nest can't resolve dependencies of the UsersService (?). Please make sure that the argument "UserRepository" at index [0] is available in the AuthModule context.

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

Error: Nest can't resolve dependencies of the UsersService (?). Please make sure that the argument "UserRepository" at index [0] is available in the AuthModule context.

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

    at Injector.lookupComponentInParentModules (D:\BackEnd\class\section11\11-02-login-authentication\node_modules\@nestjs\core\injector\injector.js:254:19)
    at Injector.resolveComponentInstance (D:\BackEnd\class\section11\11-02-login-authentication\node_modules\@nestjs\core\injector\injector.js:207:33)
    at resolveParam (D:\BackEnd\class\section11\11-02-login-authentication\node_modules\@nestjs\core\injector\injector.js:128:38)
    at async Promise.all (index 0)
    at Injector.resolveConstructorParams (D:\BackEnd\class\section11\11-02-login-authentication\node_modules\@nestjs\core\injector\injector.js:143:27)
    at Injector.loadInstance (D:\BackEnd\class\section11\11-02-login-authentication\node_modules\@nestjs\core\injector\injector.js:70:13)
    at Injector.loadProvider (D:\BackEnd\class\section11\11-02-login-authentication\node_modules\@nestjs\core\injector\injector.js:97:9)
    at D:\BackEn

yarn start:dev 실행 시, 위와 같은 오류가 발생하여, users.module.ts 파일의 exports 부분을 아래와 같이 수정하였습니다.

exports: [
        TypeOrmModule,
        //   원래는 TypeOrmModule이 아닌 => UsersService(강의 기준)
    ],

이렇게 수정하니, 문제는 해결되었는데, 이렇게 수정해도 괜찮은지, 이유가 무엇인지 잘 모르겠어 여쭤봅니다.

javascript node.js docker rest-api nestjs

답변 1

0

인프런 AI 인턴

안녕하세요, 인프런 AI 인턴입니다.
hun님, NestJS에서 발생한 의존성 주입 문제에 대해서 질문해주셨군요.

에러 메시지에 따르면 UsersService가 의존하는 UserRepositoryAuthModule 컨텍스트에서 찾을 수 없다고 합니다. 이 경우 가장 먼저 확인해야 할 것은 UserRepository가 제대로 프로바이더로 등록되어 있는지 그리고 AuthModule에 올바르게 임포트되었는지입니다.

exportsTypeOrmModule를 추가하는 대신 UsersService를 추가한 것은 TypeOrmModuleUserRepository를 내보내는 역할을 하고 있기 때문에 해결된 것으로 보입니다. 만약 TypeOrmModule 설정에서 UserRepository를 포함하고 있다면, TypeOrmModuleexports에 추가하는 것이 맞습니다. 이렇게 하면 다른 모듈에서 TypeOrmModule를 임포트할 때 UserRepository도 같이 임포트되어 사용할 수 있게 됩니다.

UsersService를 직접 exports에 추가하는 것보다는 해당 서비스가 사용하는 리포지토리나 모델을 내보내는 모듈을 추가하는 것이 올바른 방식입니다. 다른 모듈에서는 해당 서비스 대신 필요한 리포지토리를 주입받아 사용할 수 있으니까요.

종합해 보면, UserRepository를 포함하는 TypeOrmModuleexports에 추가하는 것이 적절한 해결 방법입니다. 하지만, 왜 이런 변화가 문제를 해결하는지 이해하는 것이 중요하답니다. TypeOrmModule 내에서 UserRepository가 어떻게 제공되고 있는지 코드를 살펴보시면 더 명확한 이해가 가능할 것입니다.

그래프 ql 문서 사용할때 느낌표 남는거 어떻게 없애나요?

0

90

2

강의 전체 소스 코드를 받고싶습니다.

0

82

2

fontawesome 사용 문의

0

90

2

소스 코드 부탁드립니다~

0

92

2

깃 레포지터리 소스

0

88

2

커리큘럼12.css 정렬 에 나오는 과제 정답알고싶어요

0

76

2

10-01 Entity TypeOrmModule.forRoot 에 entities

0

93

3

강의 버전관련 문의입니다

0

106

2

Ubuntu 설치 관련

0

64

1

schema.gql 질문 드립니다.

0

55

1

서버 재실행시 Many to Many

0

107

3

input 관련 문의

0

93

2

Rest API 보다는 graphql이 주된 내용인데

0

138

2

강의 전체 소스코드 받을수있을까요?

0

163

1

도커볼륨 마운트 관련

0

129

2

findOne 타입스크립트오류

0

111

1

http => htrtps 호출 인증서 신뢰 오류

0

362

1

self-signed certificate in certificate chain 에러 발생

0

424

1

mongoose 설치 오류

0

146

1

특정 API, 특정 IP 허용 (단일경로에 CORS 활성화)

0

291

2

08-06

0

182

3

구조랑 패턴 관련해서 질문

0

128

2

mydocker

0

131

2

coolsms statuscode 2000 인데 전송안돼는 경우 확인.

0

157

1