gpt4 book ai didi

javascript - NestJS/TS 中的依赖 hell

转载 作者:行者123 更新时间:2023-12-02 16:15:26 24 4
gpt4 key购买 nike

我用许多模块构建了 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com