0

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.

2
  • are you using the account called roots or are you trying to use the root account? Commented Aug 8, 2018 at 18:13
  • @awesomeguy its root account im trying to generate error so i put roots instead of root Commented Aug 8, 2018 at 18:15

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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;
        }
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.