0

I think this is easiest to explain with an example.

var my_variable = 10;

let my_sql = "select count(id) as id_count from table";

con.query(my_sql, function (err, my_result) {
    if (my_result[0].id_count > 10) {
        my_variable += 10;
    }

The problem:

The con.query callback function is not able to see the value of my_variable.

Stiofán
  • 453
  • 5
  • 17
  • 1
    It can see it just fine. The problem is than `query()` is asynchronous so the assignment `my_variable += 10` doesn't happen right away. You can only verify the assignment after `query()` has called the callback. – Mark Jul 07 '18 at 17:09
  • Thanks for your comment. If I change `my_variable += 10` to `console.log(my_variable)` I get undefined... – Stiofán Jul 07 '18 at 17:14
  • 1
    Mark, you're right. The problem is later in the code the variable was being destroyed. So the variable was being destroyed before `my_variable += 10;`... – Stiofán Jul 07 '18 at 17:24

1 Answers1

0

The problem is me. Quite a bit later on in my code the my_variable is getting destroyed. But because the query is taking so long, it is destroyed before it is used...

Stiofán
  • 453
  • 5
  • 17