Friday, 12 May 2017

Why is javascript variable undefined if it prints to console

I am trying to understand JS better. I have defined a variable test, where I attempted to update it inside the function in order for it to take the value of the list of an object contained in a variable, mySum. Despite this printing to the console correctly, mySum prints undefined when tested in the final line to see if it's updated. Can I get some guidance on what I'm doing incorrectly with respect to updating the global variable, test?

var test;
    knex("my_table").sum("funds").where("user_id",2)
    .then(function (mySum){
        return mySum;
    })
    .then(function (mySum){
        knex("my_table").select("funds").where("funds",mySum[0]['sum'])
        .bind(console)
        .then(console.log)
        test = mySum;
    });
console.log(test);



via a programmer

No comments:

Post a Comment