1

I have following code:

app.post('/', function (req, res)
    {
        /*
        connection is created with mysql
         */
        var success = true
        connection.query(sql, [values], function (err, result)
        {
            success = false
        })
        connection.end()
        console.log(success)
    }
)

The output of this is true. How do I change the value of success from inside of connection.query?

Rohan Sharma
  • 69
  • 3
  • 13
  • 2
    Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – JJJ Oct 01 '17 at 06:51

2 Answers2

1

This is Async program, your console.log() statement is getting executed before the connection.query callback function. The Value of success is being altered inside the function.

You can test this by:

app.post('/', function (req, res) {
    var success = true;
    connection.query(sql, [values], function (err, result){
       success = false;
       console.log(success);
       // more code goes here
    });
    connection.end();
});

If you want to write mode code, that'll go inside the callback function, where the comment exist.

Welcome to Node.js ;)

Himanshu Mittal
  • 584
  • 1
  • 6
  • 21
1

Though the code block in which success is redefined success = false; is "before" console.log(result) in terms of line numbers. It actually takes place after. connection.query() is invoked with a function which will get invoked at a later time, after the query has completed.

There's at least 3 different ways to handle this.

the callback style this is simply wrapping the code you want to execute in the conn.query anonymous function like so

app.post('/', function (req, res)
    {
        /*
        connection is created with mysql
        */
        var success = true;
        var callback = function(result) {
            success = result;
            console.log(success);
            connection.end()
        };

        connection.query(sql, [values], function (err, result)
        {
            callback(result);
        });
    }
);

the promise style if conn.query() returns returns a promise then you could do something like this:

app.post('/', function (req, res)
    {
        /*
        connection is created with mysql
        */
        var success = true;
        var callback = function(result) {
            success = result;
            console.log(success);
            connection.end()
        };

        connection.query(sql, [values]).then(callback);
    }
);

more info on Promises

Also, if you're using the mysql driver for node, you can also use promise-mysql to use promises.

the await style which operate more intuitively for those who aren't used to event-driven programming. Assuming your conn.query() supports Promises, you could use await and it would look something like this:

app.post('/', function (req, res)
    {
        /*
        connection is created with mysql
        */
        var success = true;
        await connection.query(sql, [values], (result) => success = false);
        console.log(success);
        connection.end();
    }
);

more info on await

Josh Bowling
  • 313
  • 3
  • 10