0

I have this class below and it seems my "getDatabaseStats()" method won't set my class' parameters. Logging "this.memberSince" within the method itself shows the correct result however the value remains null in the constructor or directly from my instance despite the method being called right before.

It doesn't seem to be a context problem as I still have access to the rest of the class' parameters and methods so I am very confused.

Edit - I just want to note that the rest of my code is asynchronous.

constructor(memberID){
    ...
    this.memberID = memberID;
    this.memberSince = null;
    this.getDatabaseStats();
    console.log(this.memberSince); // null
}

getDatabaseStats(){
    const MySecondClass = require(`path to class`);

    let sql = `My SELECT query`;
    MySecondClass.database.query(sql, (error, rows) => {
        ...
        this.memberSince = this.formatDate(rows[0].member_since);
        console.log(this.memberSince); // 2020-07-06
        console.log(this.memberID);    // My member ID as normal
    });
}

formatDate(date){
    // Returns date formatted to YYYY-MM-DD
}

1 Answers1

0

Your this.memberSince was initially set to null. and while the this.getDatabaseStats() sets it again, later on, it outputs your console log with the initial value null simply because the promise hasn't returned yet. so the order of execution happens this way:

this.memberSince = null;  <<<<<<<<FIRST
this.getDatabaseStats();  <<<<<<<<3RD BECAUSE IT WAITS FOR DATA(PROMISE)
console.log(this.memberSince); <<<<<<<<SECOND BECAUSE IT IS ALREADY QUEUED AND VALUE IS PRESENT

what I would try doing is making my function an async function to return the promise first before I use it

constructor(memberID){
    ...
    this.memberID = memberID;
    this.memberSince = await this.getDatabaseStats(); //<<<<<<<<<<THIS
    console.log(this.memberSince); // null
}

async getDatabaseStats(){  <<<<<<<<<ASYNC FUNCTION
    const MySecondClass = require(`path to class`);

    let sql = `My SELECT query`;
    MySecondClass.database.query(sql, (error, rows) => {
        ...
       
        console.log(this.memberSince); // 2020-07-06
        console.log(this.memberID);    // My member ID as normal
        return await this.formatDate(rows[0].member_since); <<<<RETURN THIS AS THE VALUE OF THE VARIABLE after await is over
    });
}

formatDate(date){
    // Returns date formatted to YYYY-MM-DD
}

just try handling it as a promise. maybe that will solve it.

Ruvin
  • 68
  • 7
  • Thanks for the answer, this would definitely work if my method only set a single parameter. However since my query has to return many values, I cannot permit myself to execute a dozen of methods with a query each. I will still mark this answer as valid since it'd definitely help for a single value. Ultimately, using a callback on this function was the preferred solution in my case. – Maxime Rioux Jul 06 '20 at 22:14
  • what other values do you want to return? NP! hope you find your answer. you could also return an array of information and just map it. Anyway hope you are guided in some way! – Ruvin Jul 06 '20 at 22:17