Tedious: Requests can only be made in the Logged In state, not the Final state -- error

Created on 7 Jan 2016  ·  4Comments  ·  Source: tediousjs/tedious

Hi everybody,

I have a problem with the database connection. It worked fine for a while, but since yesterday I'm having this error. I have no idea why.

[Request Error: Requests can only be made in the Logged In state, not the Final state]

connection.on('connect', function(err) {
        Console.log("-------here------");
    if(err) {
        return console.error(err); // <--
        connected = false;
    } else {
        connected = true;
    }
});

I think he is not getting into the callback function of connection.on. He doesn't print anything and connected is always false.

Thank you !!

Most helpful comment

I'm a newb at all of this but I found adding a handler for the connection debug event helped me with this exact error. I found out that my connection was being refused by the sql server.
connection.on('debug', function(err) { console.log('debug:', err);});

All 4 comments

I have a similar problem.

C:\Users\Austin\Desktop\prok>node prok.js
{ [RequestError: Requests can only be made in the LoggedIn state, not the Connecting state
]
  message: 'Requests can only be made in the LoggedIn state, not the Connecting state',
  code: 'EINVALIDSTATE' }
buffer.js:506
    throw new RangeError('index out of range');
          ^
RangeError: index out of range
    at checkOffset (buffer.js:506:11)
    at Buffer.readUInt8 (buffer.js:544:5)
    at Packet.isLast (C:\Users\Austin\Desktop\prok\node_modules\tedious\lib\packet.js:121:
29)
    at ReadablePacketStream.<anonymous> (C:\Users\Austin\Desktop\prok\node_modules\tedious
\lib\message-io.js:102:18)
    at ReadablePacketStream.emit (events.js:107:17)
    at readableAddChunk (C:\Users\Austin\Desktop\prok\node_modules\tedious\node_modules\re
adable-stream\lib\_stream_readable.js:201:16)
    at ReadablePacketStream.Readable.push (C:\Users\Austin\Desktop\prok\node_modules\tedio
us\node_modules\readable-stream\lib\_stream_readable.js:165:10)
    at ReadablePacketStream.Transform.push (C:\Users\Austin\Desktop\prok\node_modules\tedi
ous\node_modules\readable-stream\lib\_stream_transform.js:133:32)
    at ReadablePacketStream._transform (C:\Users\Austin\Desktop\prok\node_modules\tedious\
lib\message-io.js:68:16)
    at ReadablePacketStream.Transform._read (C:\Users\Austin\Desktop\prok\node_modules\ted
ious\node_modules\readable-stream\lib\_stream_transform.js:172:10)

I'm pretty much using the exact code form here: https://azure.microsoft.com/en-us/documentation/articles/sql-database-develop-nodejs-simple-linux/

--prok.js--

var Connection = require('tedious').Connection;
var config = {
    userName: 'tester',
    password: 'tester',
    server: 'localhost',
    options: {
        port:3306
    }
};
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to proceed.
    console.log("Connected");
});
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
function executeStatement() {
    request = new Request("SELECT * FROM testdb.testtable;", function(err) {
    if (err) {
        console.log(err);}
    });
    var result = "";
    request.on('row', function(columns) {
        console.log('row');
        columns.forEach(function(column) {
          if (column.value === null) {
            console.log('NULL');
          } else {
            result+= column.value + " ";
          }
        });
        console.log(result);
        result ="";
    });
    request.on('done', function(rowCount, more) {
    console.log(rowCount + ' rows returned');
    });
    connection.execSql(request);
}

executeStatement();

@yossazou There is not enough information here to understand what's going on. The error you see means you try to execute a request while tedious is no longer connected to the DB.

@austincap You're executing a request on the connection before the connection was established. That's why you get the Requests can only be made in the LoggedIn state, not the Connecting state error.

I'm a newb at all of this but I found adding a handler for the connection debug event helped me with this exact error. I found out that my connection was being refused by the sql server.
connection.on('debug', function(err) { console.log('debug:', err);});

var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var express = require("express");
var bodyParser = require("body-parser");
var app = express();

// Body Parser Middleware
app.use(bodyParser.json());

//Setting up server
var server = app.listen(process.env.PORT || 8081, function () {
var port = server.address().port;
console.log("App now running on port", port);
});

// Create connection to database
var config = {
authentication: {
options: {
userName: 'username', // update me
password: 'password' // update me
},
type: 'default'
},
server: 'servername.database.windows.net', // update me
options: {
database: 'development', //update me
encrypt: true//important
}
}
//create the connection to azure
var connection = new Connection(config);
//create a json array
var data = []
function executeStatement(query, res) {
var request = new Request(query, function(err, rowCount, rows) {
if (err) {
console.log(err);
} else {
console.log(rowCount + ' rows');
res.send({ status: 200, data: data, message: "OK"})
}
});

//connection.on('debug', function(err) { console.log('debug:', err);}); if the connection is not working

request.on('row', function (row) {
  data.push({
      ID: row[0].value,
      ProductName: row[1].value,
      Price: row[2].value,
      ProductDescription: row[3].value,
      CreatedDate: row[4].value,
      Username: row[5].value
  })

});
connection.execSql(request);
}

app.get('/api/products', function (req, res) {
var query =('Select * from Products');
executeStatement(query, res);
});

//i noticed that my db password and user credbtials were reset so could not make te connection

Was this page helpful?
0 / 5 - 0 ratings