1 year ago

#123383

test-img

A.G.

RequestError: Requests can only be made in the LoggedIn state, not the Connecting state

Using Tedious to connect to MSSQL DB. In the code below, I get the error:

RequestError: Requests can only be made in the LoggedIn state, not the Connecting state

exports.exec_sql_cmd = async function(cmd)
{
var config = {  
    server: 'xx.xx.xx.xx',
    authentication: {
        type: 'ntlm',
        options: {
            userName: 'master',
            password: 'masterp',
   domain: 'the.domain.com'
        }
    },
    options: {            
        encrypt: false,
        database: 'tblTest'
    }
};  
var connection = new Connection(config);
connection.on('connect', function(err) { 
    console.log("Connected");  
    executeStatement();
});
connection.connect();
 
function executeStatement() {  
var request = new Request(cmd, function(err) {  
 if (err) {  
    console.log(err);}  
});  

request.on('row', function(columns) {  
    columns.forEach(function(column) {  
      if (column.value === null) {  
        console.log('NULL');  
      } else {  
        console.log("Row Inserted");  
      }  
    });  
});

request.on("requestCompleted", function (rowCount, more) {
    connection.close();
});
connection.execSql(request);  
}

}

`

I do get connected (this line runs: console.log("Connected"); ), but then this error is thrown: RequestError: Requests can only be made in the LoggedIn state, not the Connecting state

I have also tried it this way, below, but this throws the error: ERROR ConnectionError: Failed to connect to xx.xx.xx.xx:1433 in 15000ms

var connection = new Connection(config);
connection.on('connect', function(err) { 
    console.log("I Am Connected!");  
});

connection.connect((err) => {
    if (err)
      return callback(err, null);

    const request = new Request(sql, (rerr, rowCount, rows) => {
        
      if (rerr)
        {
            console.log("THIS IS THE ERROR: " + rerr)
            return callback(rerr, null)
        }

        callback(null, {rowCount, rows});
    });

    request.on("requestCompleted", function (rowCount, more) {
        connection.close();
    });

    connection.execSql(request);
  });

Thanks in advance.

node.js

sql-server

tedious

tediousjs

0 Answers

Your Answer

Accepted video resources