본문 바로가기
Log/Trouble shoot

nestjs_ ERROR [ExceptionHandler] Nest can't resolve dependencies

by RIEM 2023. 6. 7.

[Nest] 11601  - 06/07/2023, 3:11:11 PM   ERROR [ExceptionHandler] Nest can't resolve dependencies of the MarketwatcherService (?). Please make sure that the argument MarketwatcherRepository at index [0] is available in the MarketwatcherModule context.

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

https://github.com/nestjs/nest/tree/master/sample/07-sequelize 을 참고하여 nest.js에 Sequelize ORM을 적용한 뒤 어플리케이션을 실행해보았지만 위와 같은 에러가 발생했다. index 문제는 대개 DI와 관련된 이슈인데, constructor에 주입된 Service나 Repository에 문제가 있는 것으로 보인다. 에러 문구를 잘 살펴보면 Repository가 Module 컨텍스트에서 index[0]에 정상적으로 있는지 한번 확인해봐라고 한다. 모듈 자체에서 Repository를 인식하지 못하는 것 처럼 보인다. 

Module로 가서 확인해보자.

import { Module } from '@nestjs/common';
import { MarketwatcherService } from './marketwatcher.service';
import { MarketwatcherController } from './marketwatcher.controller';
import { SequelizeModule } from '@nestjs/sequelize';
import { MarketWatcher } from './models/marketwatcher.model';

@Module({
  imports: [SequelizeModule.forFeature([MarketWatcher])],
  controllers: [MarketwatcherController],
  providers: [MarketwatcherService],
})
export class MarketwatcherModule {}

providers에 Service 레이어만 있다. 그러고보니 Repository가 모듈의 providers에 등록되어 있지 않은 상황이다.

import { Module } from '@nestjs/common';
import { MarketwatcherService } from './marketwatcher.service';
import { MarketwatcherController } from './marketwatcher.controller';
import { SequelizeModule } from '@nestjs/sequelize';
import { MarketWatcher } from './models/marketwatcher.model';
import { MarketwatcherRepository } from './marketwatcher.repository';

@Module({
  imports: [SequelizeModule.forFeature([MarketWatcher])],
  controllers: [MarketwatcherController],
  providers: [MarketwatcherService, MarketwatcherRepository],
})
export class MarketwatcherModule {}

Repository를 주입해어서 문제 해결했다.

 

댓글