I have this node api app built using express and typescript. Before starting the server I am initializing the static variable of the Configuration Class with the required config. But when I use this static variable in a separate Typescript class across application, I keep getting this undefined error.
Is there something which I am missing here?
server.ts
// Initialize Config
Configuration.initAppConfig();
server.start()
app.config.ts
import config from 'config';
const packageInfo = require('../../package.json');
// other imports
export class Configuration {
static appConfig: IAppConfig;
static initAppConfig() {
Configuration.appConfig = <IAppConfig>{
appName: packageInfo.name,
db: <IDbConfig>config.get("db"),
server: <IServerConfig>config.get("server")
}
//prevent it from getting reassigned
Object.freeze(this.appConfig);
}
}
db.config.ts
import { Configuration } from './app.config';
export class DBConfig{
private static logger = LoggerFactory.getLogger(Configuration.appConfig.appName);
// other stuffs
}
Error
private static logger = LoggerFactory.getLogger(Configuration.appConfig.appName);
^
TypeError: Cannot read property 'appName' of undefined
PS: It works if I remove static from logger. Any reason why this is happening? I am literally banging my head. :|
//works
private logger: Logger = LoggerFactory.getLogger(Configuration.appConfig.appName);
//Not working - appName of undefined error
private static logger: Logger = LoggerFactory.getLogger(Configuration.appConfig.appName);