从同一模块注入提供者:无法解决依赖关系

时间:2020-08-04 21:34:16

标签: javascript typescript dependency-injection nestjs

错误报告

当前行为

实例化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 {}

TransactionService:

@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。不幸的是,我似乎无法在示例回购中复制它。

环境

Nest version: 7.4.1

1 个答案:

答案 0 :(得分:1)

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