Reputation: 1085
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.
Upvotes: 0
Views: 54
Reputation: 1623
You can do something like this:
function connect_to_mysql(callback) {
var con = mysql.createConnection({
host: "localhost",
user: "roots",
password: "",
database: "mydb"
});
con.connect(function(err) {
if (err) {
callback(err);
} else {
callback(null, con);
}
});
}
function do_stuff() {
connect_to_mysql(function(err, connection) {
if (err) {
//abort
} else {
console.log('--------------------------------------------------');
console.log('ALL OK ## ');
return;
}
});
}
Upvotes: 0
Reputation: 640
If you want to actually display the error you should do console.log(err);
I think the main use of throw err;
is to stop the script since I ran a test with console.log(err)
but not throw err;
and it displayed the error but the program kept running.
Upvotes: 0
Reputation: 1817
The problem here is that the connect function is async, that's why it receives a callback:
con.connect(function(err) {
if (err)
{
throw err;
}
});
When you run do_stuff, console.log('--------------------------------------------------'); console.log('ALL OK ## ');
and function(err) { if (err) { throw err; } }
run at the same time.
I'd suggest changing your connect_to_mysql function to receive a callback that will be called after establishing a connection:
function connect_to_mysql( callback ){
var con = mysql.createConnection({
host: "localhost",
user: "roots",
password: "",
database: "mydb"
});
con.connect(function(err) {
if (err){
throw err;
} else {
callback(con);
}
});
}
Then you can do something like:
connect_to_mysql( conn => {
console.log("all ok");
// run queries ...
});
Your callback function could also handle errors in case the connection fails:
function connect_to_mysql( callback ){
var con = mysql.createConnection({
host: "localhost",
user: "roots",
password: "",
database: "mydb"
});
con.connect(callback);
}
connect_to_mysql( function(err,conn) {
if(err) { //abort };
console.log("all ok");
// run queries ...
});
Hope this helps
Upvotes: 2