5

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);
0

2 Answers 2

2

The main difference between the version that works (without the static) and the version that doesn't work (with static) is on the timing of when the member is being initialized.

When the member is not static, it is only initialized when some code (not displayed here) is creating an instance of the containing class (DBConfig). By that time, Configuration.appConfig has already been initialized.

When the member is static, it is initialized much earlier, at a stage when Configuration.appConfig is not initialized yet.

One way to overcome this is to ensure that Configuration.appConfig is initialized first, for example by using a synchronous call (or some other solutions, depending on the structure of your code segments not shown here).

Sign up to request clarification or add additional context in comments.

Comments

0

Regarding instance/static, instance methods belong to the instance of a class (objects created with the “new” keyword) and static methods extend the class template, meaning they belong to the class itself and are accessible to all the instances (objects) of the class.

So, if you try to call the instance method from a the static method means trying to access the instance method of an already existing instance inside the static method. In other words, you need to create/have an instance inside the scope of your static method to be able to access its instance method.

I am not sure if instance/static concept as explained here applies purely this way in typescript, but calling an instance from a spare static method does not seem a good idea taking into account the general understanding of static.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.