Nest.js循环依赖无法解析求助:forwardRef模块导入仍注入失败
NestJS循环依赖场景下NotificationsService注入失败问题
遇到如下错误:
ERROR [ExceptionHandler] Nest can't resolve dependencies of the InventoryElementsService (PrismaService, ?, AuthorizerService). Please make sure that the argument dependency at index [1] is available in the InventoryElementsModule context.
相关代码
InventoryElementsService构造函数
@Injectable() export class InventoryElementsService { constructor( private readonly prisma: PrismaService, private readonly notificationsService: NotificationsService, private readonly authorizer: AuthorizerService, ) { } ... }
InventoryElementsModule配置
@Module({ imports: [ forwardRef(() => UsersModule), forwardRef(() => UserPreferenceModule), forwardRef(() => NotificationsModule), forwardRef(() => InventoryAttributesModule), AuthorizerModule ], providers: [ InventoryElementsResolver, InventoryElementsService, PrismaService, ], exports: [InventoryElementsService] }) export class InventoryElementsModule {}
NotificationsModule配置
@Module({ imports: [ forwardRef(() => UserPreferenceModule), forwardRef(() => InventoryElementsModule), EmailModule ], providers: [ NotificationsResolver, NotificationsService, PrismaService ], exports: [NotificationsService] }) export class NotificationsModule {}
临时解决方案(非最优)
在InventoryElementsService的构造函数中通过@Inject(forwardRef(() => NotificationsService))显式注入可正常工作:
@Injectable() export class InventoryElementsService { constructor( private readonly prisma: PrismaService, @Inject(forwardRef(() => NotificationsService)) private readonly notificationsService: NotificationsService, private readonly authorizer: AuthorizerService, ) { } }
已排查过模块导入和AppModule配置,仍找不到根本问题,寻求帮助。
内容的提问来源于stack exchange,提问作者Felix Moser
相关产品推荐
相关产品推荐

