我有一个NestJs应用程序,在我对另外两个服务(PmsConnectionService-> UserService)使用依赖项注入后,先前运行的服务会引发错误(ReviewService找不到DoctorLoaderService)。所有这些都来自单独的模块。
我的nestjs版本是5.1.0。 PmsConnectionService是我新创建的模块的一部分。我不得不使用forwardRef连接到拥有DoctorLoaderService的模块。 注意,在下面的代码中,PmsConnectionService注入了DoctorClinicService,后者正在注入DoctorLoaderService。当我在PmsConnectionService中注释掉DoctorClinicService时,错误消失了。但是我仍然不知道为什么错误首先发生。
服务
@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,
) { }
模块:
@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 { }
启动时,应用程序将引发以下错误:
[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)
尽管我认为问题出在PmsConnectionService和DoctorClinicService之间的连接中,但我不知道如何解决此问题。
答案 0 :(得分:1)
看起来您只设置了一半的前向引用。由于您的ReviewService
(属于BookingModule
的一部分取决于您的DoctorLoaderService
(属于DoctorModule
的一部分)而您的DoctorLoaderService
取决于您的ReviewService
,您需要在每个受影响类的模块和服务中都forwardRef
。在这种情况下,DocotrModule
需要forwardRef(() => BookingModule)
和BookingModule
需要forwardRef(() => DoctorModule)
以及ReviewService
和DoctorLoadingService
具有相似的@Inject(forwardRef(() => )
设置
通常,最好避免这些循环依赖,因为它们会造成难以调试的麻烦。也许看看体系结构是否有变化,可以使代码更简单/更少依赖其他部分。