【问题标题】:How to expose TypeORM Repository to other modules in NestJS如何将 TypeORM 存储库暴露给 NestJS 中的其他模块
【发布时间】:2021-08-05 19:43:48
【问题描述】:

我将所有与数据库相关的操作都移到了一个独立的模块中:

@Module({
  imports: [
    TypeOrmModule.forFeature([
      PostRepository,
      UserRepository,
      CommentRepository,
    ]),
  ],
  exports: [PostRepository, UserRepository, CommentRepository],
  providers: [PostsDataInitializer],
})
export class DatabaseModule {}

但在其他模块中,当我导入DatabaseModule 并尝试在服务类中注入PostRepository 时,出现以下错误。

Nest cannot export a provider/module that is not a part of the currently processed module (DatabaseModule). 
Please verify whether the exported PostRepository is available in this particular context.

【问题讨论】:

  • 您是如何直接导入存储库的?例如,您从哪里导入 PostRepository? AFAIK TypeOrmModule.forFeature 方法将实体类数组作为第一个参数,而不是存储库数组。
  • @ErangaHeshan 自己解决了,见下文。
  • 哇...我不知道您可以直接导入存储库。 ??????
  • @ErangaHeshan 当然可以采用custom Repository 类的数组,见Nestjs docs database#custom-repository

标签: nestjs typeorm


【解决方案1】:

看了some of my Angular codes后自己解决了,只需要导出DatabaseModule中的TypeOrmModule即可。

@Module({
  imports: [
    TypeOrmModule.forFeature([
      PostRepository,
      UserRepository,
      CommentRepository,
    ]),
  ],
  exports: [TypeOrmModule],
  providers: [PostsDataInitializer],
})
export class DatabaseModule {}

【讨论】:

    猜你喜欢
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 2022-08-05
    • 2020-04-19
    • 2020-06-06
    相关资源
    最近更新 更多