NestJS ConfigService 在应用启动时的异步 TypeORM 配置期间返回未定义的值

问题描述

我正在尝试按照以下一些文档为我的 nestJS 应用设置配置:

配置:https://docs.nestjs.com/techniques/configuration

TypeORM:https://docs.nestjs.com/techniques/database#async-configuration

我已将 .env 文件添加到项目的根目录(与 package.json 级别相同),其值如下:

DB_URL=localhost
DB_USER=root
DB_PASSWORD=root
DB_NAME=test_db

在我的 app.module.ts 中,我导入了以下模块:

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule,ConfigService } from '@nestjs/config';

import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [
    ConfigModule.forRoot(),TypeOrmModule.forRootAsync({
      imports: [ConfigModule],useFactory: (configService: ConfigService) => {
        // The value logged below is undefined'
        console.log(`DB URL: ${configService.get('DB_URL')}`);
        return {
          type: 'MysqL',host: configService.get('DB_URL'),port: 3306,username: configService.get('DB_USER'),password: configService.get('DB_PASSWORD'),database: configService.get('DB_NAME'),entities: ['dist/**/*.entity{.ts,.js}'],synchronize: false,migrations: ['dist/migrations/*{.js}'],cli: {
            migrationsDir: 'migrations',},};
      },inject: [ConfigService],}),],controllers: [AppController],providers: [AppService],})
export class AppModule {}

如上所述,configService.get('DB_URL') 检索到的值是 undefined。希望获得有关如何设置配置的任何帮助/建议,以便我可以在启动应用程序时读取 .env 文件中的值。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...