最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Problem when try to inject a service into another service using nestjs - Stack Overflow

programmeradmin0浏览0评论

I'm facing a problem. I have this example repository:

I want to inject RaceService into HeroService because I want to use some methods such as getById.

I follow the documentation described here:

The problems happen when I do next:

At RaceModule I added RaceService as export:

@Module({
  imports: [TypeOrmModule.forFeature([RaceEntity])],
  controllers: [RaceController],
  providers: [RaceService],
  exports: [RaceService],
})

At HeroModule I imported RaceModule:

@Module({
  imports: [RaceModule, TypeOrmModule.forFeature([HeroEntity])],
  controllers: [HeroController],
  providers: [HeroService],
})

At HeroService I added inject for RaceService:

constructor(
    @InjectRepository(HeroEntity)
    private heroRepository: Repository<HeroEntity>,
    private readonly raceService: RaceService,
  ) {
  }

The error appears at the console:

Error: Nest cannot create the module instance. Often, this is because of a circular dependency between modules. Use forwardRef() to avoid it

I try different ways to import the service, including creating a new CommonsModule, also putting a forwardRef into the raceService definition but another error appears:

Nest can't resolve dependencies of the HeroService (HeroEntityRepository, ?). Please make sure that the argument at index [1] is available in the HeroModule context.

Thanks for the help!

I'm facing a problem. I have this example repository: https://github./pillowslept/nest-example

I want to inject RaceService into HeroService because I want to use some methods such as getById.

I follow the documentation described here: https://docs.nestjs./modules

The problems happen when I do next:

At RaceModule I added RaceService as export:

@Module({
  imports: [TypeOrmModule.forFeature([RaceEntity])],
  controllers: [RaceController],
  providers: [RaceService],
  exports: [RaceService],
})

At HeroModule I imported RaceModule:

@Module({
  imports: [RaceModule, TypeOrmModule.forFeature([HeroEntity])],
  controllers: [HeroController],
  providers: [HeroService],
})

At HeroService I added inject for RaceService:

constructor(
    @InjectRepository(HeroEntity)
    private heroRepository: Repository<HeroEntity>,
    private readonly raceService: RaceService,
  ) {
  }

The error appears at the console:

Error: Nest cannot create the module instance. Often, this is because of a circular dependency between modules. Use forwardRef() to avoid it

I try different ways to import the service, including creating a new CommonsModule, also putting a forwardRef into the raceService definition but another error appears:

Nest can't resolve dependencies of the HeroService (HeroEntityRepository, ?). Please make sure that the argument at index [1] is available in the HeroModule context.

Thanks for the help!

Share Improve this question edited Jul 27, 2019 at 4:19 BPDESILVA 2,1985 gold badges17 silver badges35 bronze badges asked Jul 27, 2019 at 3:55 Juan Camilo VelásquezJuan Camilo Velásquez 611 silver badge8 bronze badges 2
  • I made a pull request on your GitHub repo, hopefully you find it helpful. – Jackie McDoniel Commented Jul 27, 2019 at 5:57
  • 1 @Juan Wele to SO! As your problem seems to be solved, consider adding it as an answer to your own question. Others might profit from your solution and can also see that your problem is already solved. :-) – Kim Kern Commented Jul 28, 2019 at 22:23
Add a ment  | 

2 Answers 2

Reset to default 3

With some help I realize that my problem appears because I created a file index.ts at the modules folder (with the idea of export the module names at once), like this:

export { RaceModule } from './race.module';
... others

So, then when I try to import the RaceModule at HeroModule, like this:

import { RaceModule } from 'modules';

My problem appears.

I don't know why but referencing the module import directly from the index.ts file rather than:

import { RaceModule } from 'modules/race.module';

Was the problem of my code.

So, following some remendations, I created some folders and move some files and my project start working as should be.

Thanks for help and hope someone find this helpful.

You need to import module of race module in hero module

发布评论

评论列表(0)

  1. 暂无评论