tediousjs / tedious

Node TDS module for connecting to SQL Server databases.

Home Page:http://tediousjs.github.io/tedious/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

ANTGOMEZ opened this issue · comments

I see this error was talked about here: https://github.com/tediousjs/tedious/issues/458 and I tried the suggested solution, but it didn't work for me.

Here is my code:

`

    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

The context of all this is: I'm trying to perform an INSERT from AWS Lambda back down to an On-Prem MS SQL database.

Thanks in advance.

Hi @ANTGOMEZ,

I've noticed that connection.on is not handling the error parameter in the callback function. Could you try handling it in a similar fashion to executeStatement? I.e. print the error if it exists. The connection function could be missing an error coming from the database side with more details.

Your new code block is missing a connection.connect() call. Could you add that in and try it again?

Thanks for doing that. Will take a look into this. Can I get the Node version and Tedious version you are using?

Thanks for your kind help.

Node: v17.1.0
Tedious: 14.1.0

I'm not 100% sure if this is the same issue you have, but there has been an issue with NTLM authentication when using Node 17 (as Tedious does not officially support Node 17 currently). If you throw the error inside of this block:

if (err) {
    console.log("CONNECTION ERROR: "+err);
    throw err;
}

and you see an error message similar to this one: https://stackoverflow.com/questions/69692842/error-message-error0308010cdigital-envelope-routinesunsupported, Node 17 may be the issue since Node upgraded to OpenSSL v3 and there are some compatibility issues. If it is possible to downgrade to Node 16, try that.

If that does not fix your issue, can you enable debugging to see a more detailed log?

 var config = {  
        server: 'xx.xx.xx.xx',
        authentication: {
            type: 'ntlm',
            options: {
                userName: 'master',
                password: 'masterp',
	   domain: 'the.domain.com'
            }
        },
        options: {            
            encrypt: false,
            database: 'tblTest'
            // Add this to your configuration file. You can pick and choose which ones to enable. 
            debug: {
                packet: true,
                data: true,
                payload: true,
                token: true
            }
        }

    };  

Then add this event listener to output the debug messages:

  connection.on('debug', (msg) => {
    console.log(msg);
  });

Sorry for the late response. But I did switch back to Node 16 and it worked.

Thank you for your help!