Question

For example, in a socket.io connection event handleServer is the callback function:

function handleServer(s)
{
    m = new mysql();
    m.Query("blabla",function(r)
    {
        console.log(r)
    });
    m.Close();
}

mysql object is:

m = require("mysql");

function Mysql()
{
    this.mysql = m.createConnection({ blablah });
    this.mysql.connect(function(err)
    {
        // blabla
    });
}

Mysql.prototype.Query = function(q, callback)
{
    this.mysql.query(q, function(err, v)
    {
        callback(v);
    })
}

In the first function call (handleServer) the query is correctly executed, but in the successive calls this fails throwing

"m has no method 'createConnection' "

Theoretically should not create another connection?

Was it helpful?

Solution

No, you dont need to create a new instance everytime.

The problem with your code is that you are using the same global var m for two different objects.

Inside your mysql func definition you are defining the global var m as the mysql module and then you are redifining the global m as new mysql.

Try this:

var m = new mysql();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top