0

I am trying to create a class variable in ES6. However I'm having problems to access this variable in different functions of my class:

class GoogleSheet {

constructor() {
    // Our data worksheet
    let sheet = undefined;   // this works for doc.getInfo()
    this.sheet2 = undefined; // this works for getRows()

    async.series([
        function getInfoAndWorksheets(step) {
            doc.getInfo(function (err, info) {
                // this is not defined here
                sheet = info.worksheets[sheetId];
                step();
            });
        },
        function workingWithRows(step) {
            // this.getRows();
        },
    ], function (err) {
        if (err) {
            console.log('Error: ' + err);
        }
    });
}

getRows() {
    // sheet is not defined here, makes sense
    this.sheet2.getRows({
        offset: 1
        // limit: 20
        // orderby: 'col2'
    }, function (err, rows) {

    });
};

// export the class
module.exports = GoogleSheet;

I would like to use the same "sheet" variable for both occurences, but without success so far.

Beric
  • 63
  • 2
  • 9
  • There is no such thing as a "class variable" in JS. You most likely want an instance *property*, which is created and accessed with `this.`. See the duplicate on what to do about "this not [being] defined" in the callback. – Bergi Sep 27 '17 at 19:50
  • Notice that you [don't want to do asynchronous things in your constructor](https://stackoverflow.com/q/24398699/1048572) anyway (no matter whether with promises or `async.js`) – Bergi Sep 27 '17 at 19:52

0 Answers0