vikingben
vikingben

Reputation: 1652

Nodejs Tedious Mssql Invalid State

I have a virtual box running Ubuntu on my local machine. I have installed nodejs as well as tedious the mssql driver. I'm attempting to pull data from a remote mssql server from a query and return json.

I've read through quite a bit of documentation the first question I would just like answered is do you have to run Nodejs on a windows box to connect to mssql server?

(I am under the impression this is the case when using the standards nodejs windows driver.)

The second question I have stepped through all of my process and the node server is functioning fine but when I get to the mssql call I'm getting the following error:

Invalid state; requests can only be made in the LoggedIn state, not the SentLogin7WithStandardLogin state

I have not found anyone else that has run into this error with the same situation as myself. I have tried sending the request with the object formation as suggested in this fix.

How do I connect to SQL Azure from NodeJS/Tedious?

I'm still getting the same error as mentioned before. So here is the code. I'm using restify was hoping just to make a restful api our of my nodejs server and spit back json from my mssql calls.

var restify = require('restify');
var Connection = require('tedious').Connection;
var server = restify.createServer();

server.listen(8080,function(){
  console.log('%s listening at %s',server.name,server.url);
  server.get('/GetInfo',GetInfo);
});

function GetInfo(req, res, next){

  console.log('Starting GetInfo function ...');
  res.header('Content-Type:application/json');

  var config = {
    user:'awesomeusername',
    password:'awesomepassword',
    server:'coolservername',
    options:{
      encrypt:true,
      database:'TableName'
  }
};
var connection = new Connection(config);
var Request = require('tedious').Request;

var sql = "SELECT ColumnName FROM Database.dbo.TableName";
connection.on('connect',function(err){
  request = new Request(sql,function(err,rowCount){
  if(err){
    res.write(err);  
    console.log('got an error %s',err)
  }else{
     res.write(rowCount+'rows'); 
  }
  });

 request.on('row',function(columns){
 columns.forEach(function(column){
 res.write(column.value); 
 });

});

connection.execSql(request);
 });

 } 

So there you have I'm a complete novice with nodejs I thought I would give it a try. Any tips or information is appreciated. Thanks in advance.

Upvotes: 2

Views: 5893

Answers (2)

user3627284
user3627284

Reputation: 9

connection.on('connect',function(err){
    request = new Request(sql,function(err,rowCount){
    if(err) {
        res.write(err);  
        console.log('got an error %s',err)
    }
    else {
        res.write(rowCount+'rows'); 
    }
});

change above code like below :

connection.on('connect', function(err) {
    setInterval(()=>{
        console.log("connection.state interval"+JSON.stringify(connection.state));
    },2000);
    setTimeout(()=>{
        console.log("connection.state"+JSON.stringify(connection.state));
        if (connection.state === connection.STATE.LOGGED_IN) {
            executeStatement();// code where connecting query with req and res obj is written.
        }
    },2000);
});

connection.on('debug', function(err) { 
    console.log('debug:', err); // for exception
});

Upvotes: 0

Patrik Simek
Patrik Simek

Reputation: 1048

1) No, you don't have to run code on Windows machine when using Tedious. It's written in pure javascript and should work everywhere nodejs work.

2) First, I would advice you to upgrade to latest Tedious 0.2.x. It's a major release with lots of improvements. Next, I would recomend you to review your code because the snipped you posted has less opening brackets { than closing brackets }.

Basicly your problem is you're calling connection.execSql(request); before the connection was established or regardless if connection was successfull. New Tedious has some improvements in connection error handling, maybe it could help you to resolve your issue.

Upvotes: 4

Related Questions