NestJs TypeORM异步配置

时间:2019-06-17 14:45:21

标签: typescript nestjs typeorm

我正在尝试使用@nestjs/typeorm模块并使用异步配置。在我的app.module.ts中,我有以下内容:

@Module({
  controllers: [
    AppController,
  ],
  exports: [ConfigModule],
  imports: [
    ConfigModule,
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (config: ConfigService) => {
        console.log("config.getPostgresConfig(): ", config.getPostgresConfig());
        return {
          ...config.getPostgresConfig(),
          entities,
          synchronize: true,
          type: "postgres",
        } as PostgresConnectionOptions;
      },
    }),
  ],
  providers: [AppService],
})
export class AppModule {}

console.log打印正确的内容,但我不断收到此错误:

2019-06-17T14:41:35.569358700Z [Nest] 45   - 06/17/2019, 2:41 PM   [NestFactory] Starting Nest application...
2019-06-17T14:39:31.277686600Z     at Object.next (/root/node_modules/tslib/tslib.js:114:57)
2019-06-17T14:41:35.740192700Z [Nest] 45   - 06/17/2019, 2:41 PM   [TypeOrmModule] Unable to connect to the database. Retrying (1)... +170ms
2019-06-17T14:41:35.740666500Z Error: No connection options were found in any of configurations file.
2019-06-17T14:41:35.740704100Z     at ConnectionOptionsReader.<anonymous> (/root/src/connection/ConnectionOptionsReader.ts:41:19)

应该通过config服务动态地完成配置,我不明白为什么它要配置文件,为什么不能连接到数据库。

1 个答案:

答案 0 :(得分:0)

这很糟糕,我也在另一个模块中导入了TypeOrmModule.forRoot(),这引起了很多问题。