Kim K.
Kim K.

Reputation: 121

Undefined value in class method though it ís correctly set in constructor

The following is a snippet from Node.js. No Babel installed, nor will my employer do so in case that was gonna be a suggestion :)

I simplified a class to see what the issue might be, but I'm totally stuck. 2 files, 1 class.

index.js

const SESSION = 'bla bla';
const VARS = 'abc';

const Helper = require('./helper');

const helpertest = new Helper(SESSION, VARS);
const x = Helper.setProduct();

console.log('End result :', x);

helper.js

module.exports = class Helper {

    constructor(SESSION, VARS) {
        this.SESSION = SESSION;
        this.VARS = VARS;
        console.log('Hi session', this.SESSION, this.VARS); // Hi session bla bla abc
    }

    static get SESSION() {
        return this.SESSION;
    }

    static setProduct() {

        console.log('Hi setProduct', SESSION); // Uncaught ReferenceError: SESSION is not defined

    }
}

The issue is passing the SESSION to this function

static setProduct() {
    
            console.log('Hi setProduct', SESSION); // Uncaught ReferenceError: SESSION is not defined
    
        }

I have tried: this.SESSION, session, session() etc. etc.

No idea why it shows up correctly in the constructor and not in my method where i actually need it.

Please hand me some advise. Thanks in advance.

Upvotes: 0

Views: 49

Answers (1)

user2814332
user2814332

Reputation: 68

I suppose SESSION is an instance member and cannot be accessed from a static context.

You could try not using static when defining get SESSION() and setProduct.

You call would then look like helpertest.setProduct... instead of Helper.setProduct....

Upvotes: 1

Related Questions