Search code examples
javascriptnode.jsvariablesscopeglobal-variables

initializing a nodejs global variable within a function and having it equate the same calling it outside


so I am having some issues with scopes when it comes to nodejs. I am wondering how you can initialize a variable in the global space, initialize it in a function (private scope) then have it equal to what you initialized it to in the private scope when you call it anywhere in the class. One thing I have noticed is that although you can use the variable in a private scope, when it is called again outside that function it just turns undefined. How can I return the same functions initialized when it was private? This is not my issued code but an example in case the picture isn't clear

let name;
class blah {
    static blablah() {
        name = josh;
    }
    console.log(name);
    //this will return undefined and not josh
}

What I need in my context:

let genesis;
let jsonChain;

class Blockchain {
    constructor() {
        //this.chain = [Blockchain.getGenesis()];
        console.log(Blockchain.getGenesis());
}
    //down the file we find the issued function...
    static getGenesis() {
         fs.readFile(jsonRoute, 'utf-8', function(err, data) {
             if (err) throw err;
    
             jsonChain = JSON.parse(data);
             genesis = jsonChain.blocks[0].GENESIS_DATA;
             return genesis;
        });
        //returning here instead inside the callback also yields undefined
        //I want to be able to access my contents from json file through 
        //the genesis variable which is global when I return this function
        //but I cannot reach the contents of the callback
    }
}

SOLUTION: Make sure to return the promise so that the async function runs later in your program when you call it.

let genesis;
let jsonChain;

class Blockchain {
    constructor() {
        this.chain = [Blockchain.getGenesis()];
            console.log(Blockchain.getGenesis().then(function(genesisData) {
      console.log(genesisData); // Genesis block data is here.
    }, function(err) {
      // This only runs if there was an error.
      console.log(err);
    }));
        }
    //down the file we find the solved function...
  static getGenesis() {
    return new Promise(function(resolve, reject) {
      fs.readFile(jsonRoute, 'utf-8', function(err, data) {
        if(err) return reject(err);
        const jsonChain = JSON.parse(data);
        resolve(jsonChain.blocks[0].GENESIS_DATA);
      });
    });
  }
    
}
module.exports = Blockchain;

Solution

  • Now that you've added the real code, there are a number of issues illustrated there.

    1. if (err) throw err inside a plain asynchronous callback does nothing useful. Nobody can catch that error and thus this should never be used. You need real error handling there. IMO, one should never write that line of code in that circumstance.

    2. return genesis; does nothing useful. That just returns back from the fs.readFile() callback where the internals of fs.readFile() are ignoring any return value. That does not return from the higher level function.

    3. jsonChain = JSON.parse(data); does successfully assign to the higher scoped variable, but any code that is trying to use the jsonChain variable will have no idea when it was assigned a value because fs.readFile() is asynchronous and calls its callback at some future, indeterminate time. So, if you're seeing that jsonChain has no value, that's because you're looking at the variable BEFORE it gets assigned. Basically, this is an anti-pattern. You can't program with asynchronous code that way in node.js. Instead, you need to USE the asynchronous value inside the callback or you need to call some function from within the callback and pass it the value. ONLY in that circumstance do you know WHEN the value is available.

    4. If the top level problem here is that you're trying to return some value you retrieved asynchronously, then you cannot directly do that in Javascript. You can read all about that here where it explains that you have to communicate that asynchronous value back to the caller with a callback, a promise or you can also use an event.