0

I am using Typescript (2.5.3) in a backend Node.js application, and trying to figure out where my coding mistake is, in the following code. Briefly, I have one main class, and several other classes that use this main class :

DataBase ( main class ) :

  • which is a singleton ( see export let DB = DataBase.Instance; below )
  • whose role is simply to connect to a Mongo database, and set a flag if connection successful or not. This flag will be used by other classes to know whether connection was established.

ModelParametersDataBase ( one of the other classes ) :

  • this class imports the main class ( see import { DB } from './DataBase' ; below )
  • gets the connection status through the main class.

Here is my issue : My issue is that even though the connection is established once and for all (DB.dataBaseStatus = true is called), the ModelParametersDataBase gets false when calling the singleton's getter.

Here is an snippet from the DataBase.ts :

class DataBase {

  private static _instance: DataBase = undefined;

  private dataBaseStatus_  = false ;


  public static get Instance() {
    return this._instance || (this._instance = new this());
  }
  public get dataBaseStatus() {
    return this.dataBaseStatus_ ;
  }

  public set dataBaseStatus(status :boolean) {
    this.dataBaseStatus_ = status ;
  }

  public configDatabase() {

    /* Connect to Mongo DB */
    mongoose.connect(MONGODB_CONNECTION_ROOT + MONGODB_DB_NAME, options)
      .then(() => {
        DB.dataBaseStatus = true ;

        debug('CONNECTED TO DATABASE ');
      },
      err => {
        DB.dataBaseStatus = false ;
        debug('ERROR - CANNOT CONNECT TO DATABASE');
      }
      );
  }
}

/** Instantiate singleton */
export let DB = DataBase.Instance;

Ane here is how I use this singleton in the ModelParametersDataBase class :

import { DB } from './DataBase' ;

export class ModelParametersDataBase {

  public static getModelParameters(): Promise<IModelParameters> {
    return new Promise((resolve, reject) => {

      if (DB.dataBaseStatus) {
        // do stuff 
      } else {
        reject(Error(CANNOT_REACH_DB));
      }
    });
  }

}

Can you please point out where my mistake is ? Thanks.

LudoZik
  • 917
  • 1
  • 8
  • 20

1 Answers1

0

You are trying to instantiate class with this keyword from static context. Why not to do something more like this:

class DataBase {
    private static instance = new DataBase();
    public static getInstance = () => DataBase.instance;
}

export default DataBase;

And then use it like this:

import DataBase from './DataBase';
Database.getInstance();

Or even you could export single instance from file:

class DataBase {};
export default new DataBase();

EDIT: In addition you should use this keyword when setting instance variables like databaseStatus.

Kacper Wiszczuk
  • 1,809
  • 11
  • 14
  • Thanks for this answer. However, the issue remains the same. My databaseStatus_ variable si correctly set to true, but when getting the value via database instance, it is false. To be more accurate, it 'stays' at the value define by default in the class. If the default value is set to true, then I get true via database instance. – LudoZik Dec 03 '17 at 13:06
  • You should set this.databaseStatus, because it is not static. – Kacper Wiszczuk Dec 03 '17 at 13:22