I'm trying to set-up a function for connecting to MySQL so other functions can call it and get a connection in return. Here is my code based on documentation:
function connect_to_mysql(){
var con = mysql.createConnection({
host: "localhost",
user: "roots",
password: "",
database: "mydb"
});
con.connect(function(err) {
if (err)
{
throw err;
}
});
return con ;
}
function do_stuff () {
connection = connect_to_mysql();
console.log('--------------------------------------------------');
console.log('ALL OK ## ');
return ;
}
Here is the problem: let's say there is a error on connection. I get this output in terminal:
--------------------------------------------------
ALL OK ##
D:\wamp\www\pop\node_modules\mysql\lib\protocol\Parser.js:80
throw err; // Rethrow non-MySQL errors
^
Basically the code goes on and I get ALL OK ## before throw err
I want to abort the code if connection fails .... to put it very simply I want connect_to_mysql to return false if connection fails so I can abort the process.
rootsor are you trying to use the root account?