[NestJS] @Module

woogiemon·2023년 3월 7일
0
post-thumbnail
@Module({
  imports: [TypeOrmModule.forFeature([UserEntity])],
  controllers: [AuthController],
  providers: [JwtService, AuthService, UserService],
})
export class AuthModule {}

imports : 이 모듈에서 사용하기 위한 프로바이더를 가지고 있는 다른 모듈을 가져온다.
TypeOrmModule이라면 Entity를 입력하면 되는 것 같다.
providers : 이 모듈에서 사용하기 위한 @Injectable() 이 붙은 Service 를 작성한다.


삽질 1.

오류 메세지

[Nest] 14934  - 08/03/2023, 13:28:57   ERROR [ExceptionHandler] Nest can't resolve dependencies of the UserService (?). Please make sure that the argument UserEntityRepository at index [0] is available in the BoardModule context.

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

현재 상황

@Module({
  imports: [TypeOrmModule.forFeature([UserEntity]), PassportModule],
  controllers: [AuthController],
  providers: [
    JwtService,
    AuthService,
    UserService,
    LocalAuthenticationGuard,
    JwtAuthGuard,
  ],
})
export class AuthModule {}
@Module({
  imports: [TypeOrmModule.forFeature([UserEntity])],
  controllers: [UserController],
  providers: [UserService, JwtService],
  exports: [UserService],
})
export class UserModule {}
@Module({
  imports: [TypeOrmModule.forFeature([BoardEntity])],
  controllers: [BoardController],
  providers: [BoardService, JwtAuthGuard, AuthService, UserService, JwtService],
})
export class BoardModule {}

BoardModule 에 문제가 생겼다는 건 알겠다. 그래서 providers 에 @Injectable() 이 붙은 Service 를 가져다 붙였지만 해결되지 않았다.
곰곰히 생각해보니 현재 BoardModule 안에서 사용되는 Service 는 BoardService 와 JwtAuthGuard 이렇게 두개 뿐이다. 그래서 나머지를 다 지웠더니 정상 동작했다.


삽질 2.

[Nest] 84056  - 09/03/2023, 19:47:32   ERROR [ExceptionHandler] Nest can't resolve dependencies of the EmployeeService (?). Please make sure that the argument EmployeeEntityRepository at index [0] is available in the AuthModule context.

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

오류 메세지를 보면 Nest 가 EmployeeService 의 의존성을 해결하지 못하고, index 0번쨰의 EmployeeEntityRepository 인수를 AuthModule 컨텍스트에서 사용할 수 있는지 확인하라고 한다.

index 0번째는 Service 클래스의 생성자에 주입한 첫번째 인자를 뜻한다는데,

@Injectable()
export class EmployeeService {
  constructor(
    @InjectRepository(EmployeeEntity)
    private employeeRepository: Repository<EmployeeEntity>,
  ) {}
  ...
  }
}

employeeRepository.. 를 의미하는건가? 이거의 인수가 뭘 의미하는건지 모르겠다.

그래서 에러 메세지를 더 읽어보니, AuthModule 의 얘기가 반복적으로 나왔다.

@Module({
  imports: [
    TypeOrmModule.forFeature([UserEntity, EmployeeModule]),
    PassportModule,
  ],
  ...
})
export class AuthModule {}

맨 위에 적어놓고서 또 실수했다. imports: EmployeeModule이 아니라 Entity로 수정하여 해결했다.

profile
삽질 기록..

0개의 댓글