Closed bitov27 closed 5 years ago
This code is running fine for me (with different credentials).
Are you reusing the same connection
variable across multiple requests? Could you be closing the connection somewhere else?
What does it say connection.end
is when it gets the error if it's not a function?
This code is running fine for me (with different credentials).
Are you reusing the same
connection
variable across multiple requests? Could you be closing the connection somewhere else?
I call the code above multiple times by timer (every 3 seconds) It work like this:
var config = {
host : 'localhost',
user : 'root',
password : 'root',
database : 'mydb',
timezone: 'utc',
multipleStatements: true
};
function handle_games (){
var connection;
var mysql = require('promise-mysql');
mysql.createConnection(config
).then(function(conn){
connection = conn;
var sql = "Select COUNT(*) From mydb";
var result = connection.query(sql);
return result;
}).then(function(result){
connection.end();
connection = {};
}).catch(function(error){
if (connection && connection.end) connection.end();
});
}
setInterval(handle_games, 3000);
I can simulate your error if the code is like this:
var config = {
host: 'localhost',
user: 'root',
password: 'root',
database: 'mydb',
timezone: 'utc',
multipleStatements: true
};
var connection; // moved declaration outside the function
function handle_games() {
var mysql = require('promise-mysql');
mysql.createConnection(config).then(function (conn) {
connection = conn;
var sql = "Select COUNT(*) From mydb";
var result = connection.query(sql);
return result;
}).then(function (result) {
connection.end();
connection = {};
}).catch(function (error) {
if (connection && connection.end) connection.end();
});
}
setInterval(handle_games, 3000);
(with connection
declared outside the function)
The way you have it written works for me though.
@bitov27 I'm closing this issue because it's been 2 weeks since the last comment.
Hi! Thank you guys! I had decided this problem!!!
Hello,
I don't know if you resolve your problem but i have the same error using sequelize-pool
.
If you do a console.log for connection
variable you can view that end method isn't defined now.
Instead of this method you can use connection.close()
.
For anyone still on this issue, mysql.createConnection(...)
should be awaited.
Using Async/Await
const conn = await mysql.createConnection(...);
...
await conn.end();
Or using Callbacks:
mysql.createConnection(...)
.then((con) => {
...
con.end()
.then(...)
.catch(...)
} )
Hi! When I use this code:
I've got error - TypeError: connection.end is not a function Why end is not a function? My nodejs version 8.10.0 Can anybody help me? Thanks!