0

I can not get return data from mysql connection check function at nodejs. Output is always false.

var dbcheck = false;

connection.connect(function (err) {
    if(err) {

        dialog.showMessageBox(null,dbOptions, function (response) {

            if(response == 1){
                shell.openExternal("https://innoscript.co");
                app.quit();
            }

            if(response == 0){ app.quit(); }
        });
    }

    if(!err){
        dbcheck = true;
        return dbcheck;
    }

});

console.log(dbcheck);
Aung Htet Paing
  • 121
  • 1
  • 1
  • 9
  • 1
    `connection.connect()` is asynchronous, so your `console.log(dbcheck);` runs first, then the rest of the code runs. –  Feb 28 '19 at 13:27
  • Yup, because of Async Nature console.log is printing the result without actually db connection code getting executed first. – Rohit Dalal Feb 28 '19 at 13:29

1 Answers1

0

Your console.log() is executed before the connection takes place.

You should console.log() inside the callback function.

Lucian Vasile
  • 482
  • 5
  • 17