Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my variable still undefined?

lastbalance never get's defined and because of that the function getBalance never does what its supposed to. I thought this might be an issue with asynchronicity but that could only be the case before the first interval, right? Any ideas?

var lastbalance;
getBalance = function (lastbalance, callback){
  btcclient.getBalance('*', 0, function(err, balance) {
    if (err) return console.log(err);
    if (lastbalance != balance || typeof lastbalance == 'undefined'){
      console.log('Last Balance:' + lastbalance);
      var lastbalance = balance;
      updateCauses();
    }
    console.log('Balance:', balance);


    if (typeof callback=='function') callback();

  });
};

setInterval(getBalance, 2000, lastbalance);
like image 339
Kinnard Hockenhull Avatar asked Nov 06 '13 19:11

Kinnard Hockenhull


2 Answers

Two issues.

1.: You defined lastbalance as a function parameter... which created another lastbalance variable in the context of your function... which superseded the variable declared in the outer scope.

var lastbalance; // your outer variable
getBalance = function (lastbalance, callback) { // weeeee, another lastbalance
    btcclient.getBalance('*', 0, function (err, balance) {
        if (err) return console.log(err);
        if (lastbalance != balance || typeof lastbalance == 'undefined') {
            console.log('Last Balance:' + lastbalance);
            var lastbalance = balance;
            updateCauses();
        }
        console.log('Balance:', balance);
        if (typeof callback == 'function') callback();
    });
};

setInterval(getBalance, 2000, lastbalance); //passing lastbalance by value

2.: You used var to declare yet another lastbalance in your function. Don't do that; it caused the same issue described above.

var lastbalance; // your outer variable
getBalance = function (lastbalance, callback) {
    btcclient.getBalance('*', 0, function (err, balance) {
        if (err) return console.log(err);
        if (lastbalance != balance || typeof lastbalance == 'undefined') {
            console.log('Last Balance:' + lastbalance);
            var lastbalance = balance; // here you create a local lastbalance.
                                       // remove the var keyword to refer to
                                       // the original lastbalance
            updateCauses();
        }
        console.log('Balance:', balance);
        if (typeof callback == 'function') callback();

    });
};

setInterval(getBalance, 2000, lastbalance);

Finally, your code should look something like this:

var lastbalance;
getBalance = function (/*lastbalance, */callback) { // remove parameter
    btcclient.getBalance('*', 0, function (err, balance) {
        if (err) return console.log(err);
        if (lastbalance != balance || typeof lastbalance == 'undefined') {
            console.log('Last Balance:' + lastbalance);
            /*var*/ lastbalance = balance; // remove var
            updateCauses();
        }
        console.log('Balance:', balance);
        if (typeof callback == 'function') callback();
    });
};

setInterval(getBalance, 2000/*, lastbalance*/); // remove argument
like image 94
canon Avatar answered Sep 30 '22 16:09

canon


You have so many lastBalance variables knocking around!

  • one in the outer scope var lastBalance =
  • one in the getBalance function (a parameter name)
  • one in the callback function var lastBalance =

This means that when you do lastBalance = in the inner function, only the third of the variables gets set. No code ever sets the variable in the outside scope. The one in the inner scope is set to undefined every time the function is run.

I'm pretty sure you only want the one variable. You need to do something like this:

var lastbalance;
getBalance = function (callback){
  btcclient.getBalance('*', 0, function(err, balance) {
    if (err) return console.log(err);
    if (lastbalance != balance || typeof lastbalance == 'undefined'){
      console.log('Last Balance:' + lastbalance);
      lastbalance = balance;
      updateCauses();
    }
    console.log('Balance:', balance);

    if (typeof callback=='function') callback();
  });
};

setInterval(getBalance, 2000);

This may not be exactly right, as I am unfamiliar with your API, but it probably isn't far wrong.

like image 37
lonesomeday Avatar answered Sep 30 '22 16:09

lonesomeday