简体   繁体   中英

Service can't find dependency because of another services injection

I have a NestJs application where a previously working service throws an error (ReviewService can't find DoctorLoaderService) after I use dependency injection on another two services (PmsConnectionService -> UserService). All of these are from separate Modules.

My nestjs version is 5.1.0. PmsConnectionService is part of a module I newly created. I've had to use forwardRef to connect toModule holding the DoctorLoaderService. Notice in the code below that the PmsConnectionService injects DoctorClinicService, which is injecting the DoctorLoaderService. When I comment out the DoctorClinicService in PmsConnectionService, the error disappears. However I still don't know why the error has occured in the first place.

Services

@Injectable()
export class PmsConnectionService {
    constructor(
        private clinicService: DoctorClinicService,
    ) {}
@Injectable()
export class UserService {
  constructor(
    private readonly connectionService: PmsConnectionService,
  ) { }
@Injectable()
export class DoctorLoaderService {
  constructor(
    @Inject(forwardRef(() => ReviewService))
    private readonly reviewService: ReviewService,
  ) {}
@Injectable()
export class ReviewService {
  constructor(
    private readonly doctorLoader: DoctorLoaderService,
    private readonly reviewManager: ReviewManagerService,
  ) { }

Modules:

@Module({
    imports: [
        forwardRef(() => DoctorModule),
        TypeOrmModule.forFeature([
            ...
          ]),
    ],
    providers: [
        PmsConnectionService,
    ],
    exports: [
        PmsConnectionService,
    ],
})
export class PmsModule {}
@Module({
  imports: [
    PmsModule,
    TypeOrmModule.forFeature([
      User,
      ...
    ]),
  ],
  controllers: [
    UserController,
  ],
  providers: [
    UserService,
  ],
  exports: [UserService],
})
export class UserModule {
  public configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(ActivityMiddleware)
      .exclude({
        path: '*',
        method: RequestMethod.OPTIONS,
      })
      .forRoutes({ path: '*', method: RequestMethod.ALL });
  }
}
@Module({
  imports: [
    UserModule,
    TypeOrmModule.forFeature([
      User,
      DoctorProfile,
      DoctorClinic,
      ...
    ]),
    forwardRef(() => BookingModule),
  ],
  controllers: [
    DoctorProfileController,
  ],
  providers: [
    DoctorLoaderService,
    DoctorClinicService,
  ],
  exports: [
    DoctorClinicService,
    DoctorLoaderService,
  ],
})
export class DoctorModule { }
Module({
  imports: [
    PmsModule,
    UserModule,
    DoctorModule,
    TypeOrmModule.forFeature([
      ...
    ]),
  ],
  controllers: [
   ...
  ],
  providers: [
    ReviewService,
  ],
  exports: [
    ReviewService,
  ],
})
export class BookingModule { }

On start the application throws a following error:

[Nest] 17921   - 9/26/2019, 9:44:15 PM   [NestFactory] Starting Nest application...
[Nest] 17921   - 9/26/2019, 9:44:15 PM   [ExceptionHandler] Nest can't resolve dependencies of the ReviewService (?, ReviewManagerService, NotiIssuerService). Please make sure that the argument at index [0] is available in the current context. +90ms
Error: Nest can't resolve dependencies of the ReviewService (?, ReviewManagerService, NotiIssuerService). Please make sure that the argument at index [0] is available in the current context.
    at Injector.resolveSingleParam (/Users/dude/Documents/lekar/unexpected_koala_backend/node_modules/@nestjs/core/injector/injector.js:115:19)
    at Promise.all.dependencies.map (/Users/dude/Documents/lekar/unexpected_koala_backend/node_modules/@nestjs/core/injector/injector.js:85:49)
    at Array.map (<anonymous>)
    at Injector.resolveConstructorParams (/Users/dude/Documents/lekar/unexpected_koala_backend/node_modules/@nestjs/core/injector/injector.js:83:58)
    at Injector.loadInstance (/Users/dude/Documents/lekar/unexpected_koala_backend/node_modules/@nestjs/core/injector/injector.js:63:20)
    at Injector.loadInstanceOfComponent (/Users/dude/Documents/lekar/unexpected_koala_backend/node_modules/@nestjs/core/injector/injector.js:40:20)
    at Promise.all.map (/Users/dude/Documents/lekar/unexpected_koala_backend/node_modules/@nestjs/core/injector/instance-loader.js:39:102)
    at Array.map (<anonymous>)
    at InstanceLoader.createInstancesOfComponents (/Users/dude/Documents/lekar/unexpected_koala_backend/node_modules/@nestjs/core/injector/instance-loader.js:39:59)
    at Promise.all.map (/Users/dude/Documents/lekar/unexpected_koala_backend/node_modules/@nestjs/core/injector/instance-loader.js:26:24)

I can't figure out how to fix this though I think the problem lies within the connection between PmsConnectionService and DoctorClinicService.

It looks like you only have half of your forward ref set up. As your ReviewService (part of the BookingModule ) depends on your DoctorLoaderService (part of the DoctorModule ) and your DoctorLoaderService depends on your ReviewService , you need to forwardRef in both the module and service of each affected class. In this case DocotrModule needs a forwardRef(() => BookingModule) and BookingModule needs a forwardRef(() => DoctorModule) as well as ReviewService and DoctorLoadingService having similar @Inject(forwardRef(() => ) setups.

In general it is best to avoid these circular dependencies as they can create headaches that are difficult to debug. Maybe see if there is a change in architecture you can take to make the code simpler/less reliant on other parts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM