0

我的项目中有两个数据库连接,因此我必须按照文档的建议命名其中一个。使用此命名连接的模块无法解析我实体的存储库的依赖关系,我无法找出原因。

我相信这与连接的“命名”有关,因为当我命名主连接时,其他存储库也有同样的问题。

感谢任何帮助!

错误:

Nest can't resolve dependencies of the DataFactService (?). Please make sure that the argument DataFactRepository at index [0] is available in the DataFactModule context.

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

模块:

@Module({
    imports: [TypeOrmModule.forFeature([DataFact], 'sumarized')],
    controllers: [DataFactController],
    providers: [DataFactService],
})
export class DataFactModule {}

服务:

@Injectable()
export class DataFactService {
    constructor(
        @InjectRepository(DataFact)
        private dataFactRepository: Repository<DataFact>,
    ) {}
}

数据库连接

@Module({
    imports: [
        TypeOrmModule.forRootAsync({
            name: 'sumarized',
            imports: [ConfigModule],
            inject: [ConfigService],
            useFactory: (configService: ConfigService) => ({
                synchronize: false,
                type: 'mysql',
                host: configService.get('DB_HOST'),
                port: configService.get('DB_PORT'),
                username: configService.get('DB_USER'),
                password: configService.get('DB_PASSWORD'),
                database: configService.get('DB_SUMARIZED_DATABASE'),
                entities: [__dirname + '/../**/*.entity.{js,ts}'],
            }),
        }),
    ],
})
export class SumarizedDatabaseModule {}

应用模块:

@Module({
    imports: [
        ConfigModule.forRoot({
            envFilePath: `.env.${process.env.NODE_ENV}`,
            isGlobal: true,
        }),
        WinstonModule.forRoot(winstonConfig),
        UsersModule,
        AuthModule,
        StoresEventsModule,
        StationsModule,
        DatabaseModule,
        MailerModule,
        SumarizedDatabaseModule,
        DataFactModule,
        SumarizedEventsModule,
    ],
    controllers: [],
    providers: [
        {
            provide: APP_INTERCEPTOR,
            useClass: LoggerInterceptor,
        },
    ],
})
export class AppModule {}
4

1 回答 1

1

@InjectRepository(EntityClass, databaseName)如果您从连接而不是默认注入实体,则需要使用。这在文档中提到

使用此设置,您必须告诉TypeOrmModule.forFeature()方法和@InjectRepository()装饰器应该使用哪个连接。如果您不传递任何连接名称,则使用默认连接。

于 2021-02-12T19:13:13.600 回答