javascript - NestJS/TS 中的依赖 hell

标签 javascript typescript nestjs

我用许多模块构建了 NestJS 项目,最近我有点迷失在其中,我正在做的最后一件事是将 QueueService 添加到我的 ProjectServiceProjectModule,但是在启动整个应用程序之后,编译器向我抛出这个:

Error: Nest can't resolve dependencies of the QueueService (UtilsService, UserService, Connection, ?). Please make sure that the argument Object at index [3] is available in the ProjectModule context.

QueueService 索引 [3] 处的参数是 ProjectService,所以他们为什么要我将 ProjectModule/ProjectService 导入我的项目模块? :P

这是我的全部代码:

@Injectable()
export class ProjectService {
    constructor(
        private conn: Connection,
        private utilsService: UtilsService,
        private userService: UserService,
        private notificationService: NotificationsService,
        private queueService: QueueService 
    ) { }
@Module({
  imports: [
    PassportModule.register({ defaultStrategy: 'jwt'})
  ],
  providers: [ProjectService, UtilsService, UserService, NotificationsService, QueueService ],
  controllers: [ProjectController],
  exports: [ProjectService]
})
export class ProjectModule {}
@Injectable()
export class QueueService {
    constructor(
        readonly conn: Connection,
        readonly utilsService: UtilsService,
        readonly userService: UserService,
        readonly projectService: ProjectService
    ){}
}
@Module({
  imports: [
    AuthModule,
    PassportModule.register({ defaultStrategy: 'jwt'})],
  providers: [QueueService , UtilsService, UserService, NotificationsService, ProjectService],
  controllers: [QueueController ],
  exports: [PassportModule]
})
export class QueueModule {}

应用模块

@Module({
  imports: [
    ...,
    TypeOrmModule.forRootAsync({
      useClass: TypeOrmConfigService
    }),
    PassportModule.register({ defaultStrategy: 'jwt'}),
    JwtModule.register({
        secret: 'secretKey'
    }),
    ScheduleModule.forRoot(),
    ...,
    ...,
    QueueModule,
    ...,
    ...,
    ProjectModule,
    ...,
    ...,
    ...
  ],
  controllers: [..., ..., ..., ..., ...],
  providers: [ ..., ..., ..., ..., ...,ProjectService, ..., ..., QueueService],
})
export class AppModule {}

感谢您的帮助,我在这里停留了 3-4 小时,我不知道还能做些什么:(

//////////////////////////////////

最佳答案

好的,这里有很多东西要解压。所以你有 circular dependencies在你的服务和你的模块之间应该有循环依赖。现在正在发生的事情是您正在创建 QueueService 的两个实例和 ProjectService 的两个实例。但是,由于这些服务相互依赖,因此 Nest 无法正确实例化它们(至少您目前拥有的方式)。

因此,这里快速简便的解决方法是为循环依赖类添加适当的 forwardRef

队列服务

@Injectable()
export class QueueService {
    constructor(
        readonly conn: Connection,
        readonly utilsService: UtilsService,
        readonly userService: UserService,
        @Inject(forwardRef(() => ProjectService))
        readonly projectService: ProjectService
    ){}
}

项目服务

@Injectable()
export class ProjectService {
    constructor(
        private conn: Connection,
        private utilsService: UtilsService,
        private userService: UserService,
        private notificationService: NotificationsService,
        @Inject(forwardref(() => QueueService))
        private queueService: QueueService 
    ) { }

现在,如果这就是您想要的,很好,请随时停止阅读并继续您的业务。


那么应该发生什么呢?如果您想要每个服务的真正单例(服务只创建一次),您应该通过它们所属的模块共享提供者并导入模块,而不是创建新的 provider 值。由于您的 ProjectModuleQueueModule 是循环依赖的,因此您需要再次使用 forwardRef。上面的服务仍然有效,所以我不用担心重写这些服务,但是您的模块应该如下所示:

队列模块

@Module({
  imports: [
    AuthModule,
    forwardRef(() => ProjectModule),
    PassportModule.register({ defaultStrategy: 'jwt'})],
  providers: [QueueService , UtilsService, UserService, NotificationsService],
  controllers: [QueueController ],
  exports: [PassportModule, QueueService]
})
export class QueueModule {}

项目模块

@Module({
  imports: [
    forwardref(() => QueueModule),
    PassportModule.register({ defaultStrategy: 'jwt'})
  ],
  providers: [ProjectService, UtilsService, UserService, NotificationsService ],
  controllers: [ProjectController],
  exports: [ProjectService]
})
export class ProjectModule {}

您绝对应该查看您的 UtilsModuleNotificationsModuleUserModule 以清理它们的导出,并重新使用该模块而不是新的提供者实例也是如此。

关于javascript - NestJS/TS 中的依赖 hell ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66879023/

相关文章:

javascript - 如何在选择下拉列表中包含空格

angular - 检查 child 是否存在不使用 AngularFire2

angular - 如何在 Ionic 2 中设置单选按钮警报的值

html - 在 Angular2 中选中复选框时启动事件

javascript - 为什么 dto 中的类型在 swagger 中不可见?

javascript - 不确定 .on() 方法

javascript - 如何使用jquery限制用户不允许低于18岁?

javascript - Angular 4 等待 promise/observable 进入循环

node.js - 如何实现对帐户激活链接的过期?

javascript - Nest.js 中的可选身份验证 @nestjs/passport