Requests can only be made in the LoggedIn state, not the SentClientRequest state

Tuhin picture Tuhin · Mar 1, 2017 · Viewed 11.7k times · Source

I am trying to get data from database but getting following error { message: 'Requests can only be made in the LoggedIn state, not the SentClientRequest state', code: 'EINVALIDSTATE' }. If I call only one query function then it's working but not for multiple

Below is my code

var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to proceed.
if(!err)
{
    console.log("Connected");
    executeStatement();
    executeStatement2();
}
else
{
    console.log(err);
}
});

var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;

function executeStatement() {
request = new Request("Select p.product_name, s.product_id, s.price,   s.create_date, s.update_date from products p left join sale s on (p.id = s.product_id) order by CONVERT(datetime, s.update_date ) desc ;", function(err) {
    if (err) {
        console.log(err);}
});

request.on('row', function(columns) {
        var rowObject ={};
        columns.forEach(function(column) {
            rowObject[column.metadata.colName] = column.value;
        });
        var jsonArray = [];
        jsonArray.push(rowObject);

});

connection.execSql(request);
}


function executeStatement2() {
request = new Request("Select * from products ;", function(err) {
    if (err) {
        console.log(err);
    }
});

request.on('row', function(columns) {
    var rowObject ={};
    columns.forEach(function(column) {
        rowObject[column.metadata.colName] = column.value;
    });
    var jsonArray2 = [];
    jsonArray2.push(rowObject);

});
 connection.execSql(request);
}

Answer

Mojtaba Ahmadi picture Mojtaba Ahmadi · Nov 8, 2017

I had a same problem. You can't run two different query requests. The solution is using from one query request and

request.on('requestCompleted', function () { // Next SQL statement. });

It is mentioned in this issue: https://github.com/tediousjs/tedious/issues/458