Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"ConnectionError: Already connecting to database! Call close before connecting to different database" connecting SQL server with nodejs

Below is the code i am using ,Some times it does not enter .then (function() ,Please let me know how to close the existing connection , I have tried various options like conn.close() after connecting, but it still dint work.

var sql = require("C:/Protractor_Scripts/node_modules/mssql");

function getEmp() {
    console.log('results'); 
    var conn = new sql.Connection("Server=, 1433;Database=;User Id=;Password=;Encrypt=true");
    console.log(conn); 
    console.log(conn.connect());
    conn.connect().then(function () { 
        console.log(conn.connect()); 
        var req = new sql.Request(conn); 
        req.query("select * from").then(function (recordset) {
            console.log(recordset);   
            conn.close();
        }).catch(function (err) {
            console.log(err);   
            conn.close();
        }); 
    }).catch(function (err) {
        console.log(err);  
    });
}

getEmp();

Below is the full error message

[ConnectionError: Already connecting to database! Call close before connecting to different database.]

  name: 'ConnectionError',

message: 'Already connecting to database! Call close before connecting to different database.',

  code: 'EALREADYCONNECTING' }
like image 257
Dilip Reddy Avatar asked Nov 08 '22 21:11

Dilip Reddy


1 Answers

Try removing multiple conn.connect() calls and have only one call.

like image 112
Manish Prasad Avatar answered Nov 15 '22 07:11

Manish Prasad