Nest 无法解析 X 的依赖关系。请确保索引 [1] 处的参数 HttpService 在模块上下文中可用

SPR*_*1NG 2 javascript node.js nestjs

我在使用 Nestjs HttpModule 时遇到问题

错误: Nest can't resolve dependencies of the ShopService (PrismaService, ?). Please make sure that the argument HttpService at index [1] is available in the QaModule context.

我的商店模块:

@Module({
    imports: [HttpModule],
  controllers: [ShopController],
  providers: [ShopService, PrismaService],
})

export class ShopModule {}
Run Code Online (Sandbox Code Playgroud)

我的 Qa 模块:

@Module({
  controllers: [QaController],
  providers: [QaService, PrismaService, ShopService],
})
export class QaModule {}
Run Code Online (Sandbox Code Playgroud)

解决办法有哪些?

Jay*_*iel 6

将您的更改ShopModule

@Module({
  imports: [HttpModule],
  controllers: [ShopController],
  providers: [ShopService, PrismaService], // Prisma Service should probably come from PrisamModule
  exports: [ShopService],
})

export class ShopModule {}
Run Code Online (Sandbox Code Playgroud)

和你QaModule

@Module({
  imports: [ShopModule],
  controllers: [QaController],
  providers: [QaService, PrismaService], // Prisma Service should still probably come from `PrismaModule`
})
export class QaModule {}
Run Code Online (Sandbox Code Playgroud)

服务只能在一个模块中声明,如果需要在其他地方使用,则应从该模块导出。在声明消费服务的模块中,应将原始模块添加到数组中imports。这将确保您只有每个非瞬态提供程序的一个实例,并且不需要在使用它的每个模块中重新创建