0

I am trying to setup a connection to my MySQL database and so far it is working, but when i run the nodejs application, and i don't read/update anything in my database the connection shuts down with:

Error: Connection lost The server closed the connection

So i searched on google and found this stackoverflow question. But i cannot get it to work, and i think it is because i am working with a class and not a function.

Below is the code i currently have:

class ConnectionFactory {

    constructor() {
        this.connectAndHandleDisconnect();
    }

    connectAndHandleDisconnect() {
        connection = mysql.createConnection(db_config);

        connection.connect(function(err) {
            if(err) {
                console.log('error when connecting to db:', err);
                setTimeout(connectAndHandleDisconnect, 2000);
            }
        });

        connection.on('error', function(err) {
            console.log('db error', err);
            if(err.code === 'PROTOCOL_CONNECTION_LOST') {
                connectAndHandleDisconnect();              // Error pops up from this line
            } else {
                throw err;
            }
        });
    }
}

And i am getting the following error message:

ReferenceError: connectAndHandleDisconnect is not defined

I have tried to replace the connectAndHandleDisconnect() to this.connectAndHandleDisconnect() and self.connectAndHandleDisconnect() but i have no clue how to find the fix to this error.

Joost Kaal
  • 353
  • 1
  • 3
  • 13
  • Odds are it didn't work when you used `this` because you used `this` inside of `function(err)`, but the problem is `this` in that scope refers to `function(err)`, not to `ConnectionFactory`, so `(function(err){/* ...*/}).connectAndHandleDisconnect` doesn't exist. You need to bind `this` outside of the function body. Similarly, the `self` approach wouldn't work if you did not bind `self` outside the function body. – gabriel.hayes Jan 21 '20 at 18:02
  • The reason it works in the example you gave in your question is because they have declared `handleDisconnect` in the global scope, you haven't declared it in the global scope, so it needs to be qualified based on its scope as an instance member. – gabriel.hayes Jan 21 '20 at 18:04

1 Answers1

1

You need to access methods via this (or with a reference to the instance that has the method) like so:

class ConnectionFactory {

    constructor() {
        this.connectAndHandleDisconnect();
    }

    connectAndHandleDisconnect() {
        connection = mysql.createConnection(db_config);
        const self = this;
        connection.connect(function(err) {
            if(err) {
                console.log('error when connecting to db:', err);
                setTimeout(self.connectAndHandleDisconnect, 2000);
            }
        });

        connection.on('error', function(err) {
            console.log('db error', err);
            if(err.code === 'PROTOCOL_CONNECTION_LOST') {
                self.connectAndHandleDisconnect();              // Error pops up from this line
            } else {
                throw err;
            }
        });
    }
}
gabriel.hayes
  • 2,267
  • 12
  • 15