【问题标题】:Injection of a provider from the same module: can't resolve dependencies从同一模块注入提供程序:无法解决依赖关系
【发布时间】:2020-11-25 01:14:06
【问题描述】:

错误报告

当前行为

实例化 PaymentProcessorModule 时出错:

Error: Nest can't resolve dependencies of the PaymentProcessor (?, PaymentsService, ProcessingService). Please make sure that the argument TransactionsService at index [0] is available in the PaymentProcessor context.

Potential solutions:
- If TransactionsService is a provider, is it part of the current PaymentProcessor?
- If TransactionsService is exported from a separate @Module, is that module imported within PaymentProcessor?
  @Module({
    imports: [ /* the Module containing TransactionsService */ ]
  })

但是,这两个服务来自同一个模块。

输入代码

这是我的模块:

@Module({
  imports: [
    TypeOrmModule.forFeature([ Transaction ]),
  ],
  providers: [
    PaymentProcessor,
    TransactionsService,

    TransactionsResolver,
  ],
  exports: [PaymentProcessor],
})
export class PaymentProcessorModule {}

事务服务:

@Injectable()
export class TransactionsService {
  constructor(
    @InjectRepository(Transaction) private transRepo: Repository<Transaction>,
  ) {}

  //...
}

最后是 PaymentProcessor:

@Injectable()
export class PaymentProcessor {
  constructor(
    private transactions: TransactionsService,
    private payments: PaymentsService,
    private processor: ProcessingService,
  ) {}

  //...
}

预期行为

预计会注入 TransactionsService。不幸的是,我似乎无法在示例 repo 中重现它。

环境

Nest version: 7.4.1

【问题讨论】:

    标签: javascript typescript dependency-injection nestjs


    【解决方案1】:

    NestJS 的官方支持告诉我PaymentProcessor 必须在imports 数组的某处提及。我检查了该类的用法,这是真的,我不小心在另一个上下文中导入了提供程序而不是模块。

    【讨论】:

      猜你喜欢
      • 2018-11-08
      • 1970-01-01
      • 2019-11-25
      • 2020-10-24
      • 2016-02-16
      • 2017-02-20
      相关资源
      最近更新 更多