NestJs-TypeORM配置可以工作,但不能与ConfigService一起工作

NestJs-TypeORM配置可以工作,但不能与ConfigService一起工作,nestjs,typeorm,configmodule,Nestjs,Typeorm,Configmodule,我想用NestJs和TypeORM创建一个RESTAPI。在myapp.module.ts中,我加载TypeForm模块 @Module({ imports: [ TypeOrmModule.forRoot({ type: 'postgres', host: 'localhost', port: 5432, username: 'postgres', password: 'postgres', database:

我想用NestJs和TypeORM创建一个RESTAPI。在myapp.module.ts中,我加载TypeForm模块

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'postgres',
      host: 'localhost',
      port: 5432,
      username: 'postgres',
      password: 'postgres',
      database: 'api',
      entities: [`${__dirname}/**/*.entity.{ts,js}`],
      synchronize: true,
    }),
  ],
})
export class AppModule {}
现在一切正常。我想从一个外部.env文件加载配置,以便从文档中加载

从这里开始

我在根项目目录中创建了一个.env文件,其中包含以下内容

DATABASE_TYPE = postgres
DATABASE_HOST = localhost
DATABASE_PORT = 5432
DATABASE_USERNAME = postgres
DATABASE_PASSWORD = postgres
DATABASE_NAME = api
DATABASE_SYNCHRONIZE = true
接下来,我将代码更新为

@Module({
  imports: [
    ConfigModule.forRoot(),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        type: configService.get<any>('DATABASE_TYPE'),
        host: configService.get<string>('DATABASE_HOST'),
        port: configService.get<number>('DATABASE_PORT'),
        username: configService.get<string>('DATABASE_USERNAME'),
        password: configService.get<string>('DATABASE_PASSWORD'),
        database: configService.get<string>('DATABASE_NAME'),
        entities: [`${__dirname}/**/*.entity.{ts,js}`],
        synchronize: configService.get<boolean>('DATABASE_SYNCHRONIZE'),
      }),
      inject: [ConfigService],
    }),
  ],
})
export class AppModule {}
当我在引导函数的main.ts中记录配置时,我从.env文件中获得了正确的配置


如何修复错误?

需要发生以下两种情况之一:

1) 通过将
isGlobal:true
选项传递到
ConfigModule.forRoot()
,您需要将
ConfigModule
设置为全局。如果执行此操作,则可以删除
TypeormModule.forRootAsync()
中的导入(然后它是一个全局模块,可以在任何地方使用它的提供程序)


2) 制作另一个模块(
MyConfigModule
或其他什么),该模块
导入
ConfigModule
及其配置,并
导出
cofinigmodule
。然后,您可以在
AppModule
中将
ConfigModule.forRoot()
更改为
MyConfigModule
,并可以在
TypeormModule.forRootAsync()
配置中将
imports:[ConfigModule]
更改为
imports:[MyConfigModule]
导入:
中的
类型或mmodule.forRootAsync()
代码翻译。

typeorm.config.ts

从'@nestjs/config'导入{ConfigModule,ConfigService};
从'@nestjs/typeorm'导入{TypeOrmModuleAsyncOptions,TypeOrmModuleOptions};
从“typeorm”导入{LoggerOptions};
导出默认类TypeOrmConfig{
静态getOrmConfig(configService:configService):类型OrmModule选项{
返回{
类型:“postgres”,
主机:configService.get('DB_host')| |'localhost',
端口:configService.get('DB_port')| | 5432,
用户名:configService.get('DB_username'),
密码:configService.get('DB_password'),
数据库:configService.get('DB_NAME'),
实体:[[uuuu dirname+'/../**.entity{.ts、.js}'],
同步:configService.get('TYPEORM_synchronize')| | false,
日志记录:configService.get('TYPEORM_logging')| | false
};
}
}
导出常量typeOrmConfigAsync:TypeOrmModuleAsyncOptions={
导入:[ConfigModule],
useFactory:async(configService:configService):Promise=>TypeOrmConfig.getOrmConfig(configService),
注入:[配置服务]
};
app.module.ts

从'./login/login.module'导入{LoginModule};
从“缓存管理器redis store”导入*作为redisStore;
从“./service/service.module”导入{ServiceModule};
从“./user/user.module”导入{UserModule};
从'@nestjs/common'导入{Module};
从'@nestjs/typeorm'导入{TypeOrmModule};
从“./app.controller”导入{AppController};
从“/app.service”导入{AppService};
从“./redis cache/redis cache.module”导入{RedisCacheModule};
从“./config/typeorm.config”导入{typeOrmConfigAsync};
从'@nestjs/config'导入{ConfigModule};
@模块({
进口:[
ConfigModule.forRoot({isGlobal:true}),
TypeOrmModule.forRootAsync(typeOrmConfigAsync),
用户模块,
服务模块,
LoginModule,
再缓存模块,
],
控制器:[AppController],
提供者:[AppService],
})
导出类AppModule{}


请添加您的configService文件@Selmi抱歉,我没有configService文件。我只需要注射它,正如你在文档中看到的那样
[Nest] 28257   - 01/06/2020, 7:19:20 AM   [ExceptionHandler] Nest can't resolve dependencies of the TypeOrmModuleOptions (?). Please make sure that the argument ConfigService at index [0] is available in the TypeOrmCoreModule context.

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