Reputation: 454
When i am closing database connection in node.js i am getting this error
Cannot enqueue Query after invoking quit
.
Here is my code
socket.on('adminConnect', function (email) {
connection = mysql.createConnection(db_config); // db_config has all details of database
connection.connect(); // no problem in connection
connection.query("SELECT id FROM user WHERE email = '" + email + "' ", function (err, results, fields) {
if (err) throw err;
if (results[0]) {
// Some code
connection.end(); // its giving error "Cannot enqueue Query after invoking quit."
} else {
console.log("no id");
}
});
});
Upvotes: 5
Views: 35829
Reputation: 8945
In general, you reuse connections instead of opening/closing all the time.
To answer your question, this is how:
connection.end();
Try putting the line outside the callback, because all the queries must complete before ending a connection, so you're safe like this:
connection.query(.......);
connection.end();
Your code would then be:
socket.on('adminConnect', function (email) {
connection = mysql.createConnection(db_config); // db_config has all details of database
connection.connect(); // no problem in connection
connection.query("SELECT id FROM user WHERE email = '" + email + "' ", function (err, results, fields) {
if (err) throw err;
if (results[0]) {
// Some code
} else {
console.log("no id");
}
});
connection.end();
});
Upvotes: 6