【发布时间】: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? AFAIKTypeOrmModule.forFeature方法将实体类数组作为第一个参数,而不是存储库数组。 -
@ErangaHeshan 自己解决了,见下文。
-
哇...我不知道您可以直接导入存储库。 ??????
-
@ErangaHeshan 当然可以采用custom Repository 类的数组,见Nestjs docs database#custom-repository。