CodeFoodPixels / node-promise-mysql

A wrapper for mysqljs/mysql that wraps function calls with Bluebird promises.
MIT License
338 stars 64 forks source link

TypeError: connection.end is not a function #102

Closed bitov27 closed 5 years ago

bitov27 commented 5 years ago

Hi! When I use this code:

var config = {
    host     : 'localhost',
    user     : 'root',
    password : 'root',
    database : 'mydb',
    timezone: 'utc',
    multipleStatements: true  
  }; 
    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();  
    }); 

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!

move-zig commented 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?

CodeFoodPixels commented 5 years ago

What does it say connection.end is when it gets the error if it's not a function?

bitov27 commented 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?

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); 
move-zig commented 5 years ago

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.

CodeFoodPixels commented 5 years ago

@bitov27 I'm closing this issue because it's been 2 weeks since the last comment.

bitov27 commented 5 years ago

Hi! Thank you guys! I had decided this problem!!!

ghost commented 3 years ago

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().

JDeepD commented 6 months ago

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(...)
    } )