#Config Service value is undefined

1 messages · Page 1 of 1 (latest)

charred trail
#

I had recently upgraded my typeorm version and was following this post (https://wanago.io/2022/07/25/api-nestjs-database-migrations-typeorm/) to set up the migration mechanism

As mentioned in the post, i have a similar typeOrm config file which looks like the following

import { DataSource } from 'typeorm'
import { ConfigService } from '@nestjs/config'
import { config } from 'dotenv'

config()

const configService = new ConfigService()

console.log('Config', configService.get('TYPEORM_HOST'))

export default new DataSource({
  type: 'postgres',
  host: configService.get('TYPEORM_HOST'),
  port: configService.get('TYPEORM_PORT'),
  username: configService.get('TYPEORM_USERNAME'),
  password: configService.get('TYPEORM_PASSWORD'),
  database: configService.get('TYPEORM_DATABASE'),
  entities: [__dirname + '/../**/*.entity{.ts,.js}'],
  synchronize: configService.get('TYPEORM_SYNCHRONIZE'),
  logging: configService.get('TYPEORM_LOGGING'),
  migrations: [configService.get('TYPEORM_MIGRATIONS')],
  migrationsRun: configService.get('TYPEORM_MIGRATIONS_RUN'),
  // subscribers: [configService.get('TYPEORM_SUBSCRIBERS')],
})

Upon inspecting the console statement the config values don't get picked up, any ideas on what is missing here? I even tried providing a path to the config() function and still the same

We go through the idea of database migrations and learn how to perform them with TypeORM.

slender turret
#

right, the ConfigService doesn't load values until the ConfigModule is bootstrapped, which does not happen in your case. I would recommend using plain process.env in this case

charred trail
#

@slender turret so tried executing it with process.env and it still results in undefined i am not sure what i am missing here

slender turret
#

very strange, do you use a .env file, or does it have a different name?

cinder geyser
charred trail