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

I'm facing a problem.I have this example repository: I want to inject RaceService into HeroServic

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745162894a4614476.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信