如何将 TypeORM Repository 暴露给 NestJS 中的其他模块

时间:2021-05-16 08:32:46

标签: nestjs typeorm

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

@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.

1 个答案:

答案 0 :(得分:1)

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

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