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

Nothing happens after execSql

arch-linux opened this issue · comments

After confirming that my MSSQL server is functioning using DataGrip & MSSQL Management Studio on Windows, Tedious does not appear to be functioning whatsoever when attempting to query my server.

// Attempt 2
var Connection = require('tedious').Connection;
var colors = require('colors');
var Request = require('tedious').Request;

var startInvoice;

var config = {
    server: "foobar_IP_address",
    authentication: {
      type: "default",
      options: {  
        userName: 'foo', 
        password: 'bar'
      }
    },
    options: {
        port: 32001,
        encrypt: false,
        database: 'foobar_sql',
        trustServerCertificate: true
    }
  };


  var connection = new Connection(config);
  connection.connect();


  connection.on('connect', function(err) {
      console.log('Attempting to connect to: ' + config.server + ':'.green + config.options.port);
    if(err) {
      console.log('Error: '.red, err)
    }
    // If no error, then good to go...
 console.log("POS Connection Established");
    // action here
   doTheThing();
  });





  function doTheThing(){
        request = new Request('select * from foobar_db', (err, rowcount, rows) => {
            if(err){ 
                throw err;
            } else {
                console.log('Request Sent');
                connection.close();
            }

            request.on('row', (columns) => {
                columns.forEach(column => {
                    console.log(column);
                }) 
            })

            request.on('doneProc' , function(rowCount,more,returnStatus,rows) {
                console.log('here');
            });

            request.on('requestCompleted', (rowCount, more)=> {
                connection.close();
            })

        });

        connection.execSql(request);

  }

When the script executes I get the following response indicating that tedious has actually established a connection with my server, however no other output after this point.

image

Any help is appreciated.
Again -- I have confirmed the following.

  • MSSQL is actually operating as expected. (confirmed using the exact same sql query into datagrip
  • I have updated to the latest tedious (and even deleted my node_modules folder and reinstalled tedious

I am building this on MacOS if it matters.

Thanks for the help!

Hi @arch-linux, try moving the event handlers outside of the new Request callback function, like this:

function doTheThing() {
  request = new Request('select * from test_tvp', (err, rowcount, rows) => {
    if (err) {
      throw err;
    } else {
      console.log('Request Sent');
      connection.close();
    }
  });

  request.on('row', (columns) => {
    columns.forEach((column) => {
      console.log(column);
    });
  });

  request.on('doneProc', function(rowCount, more, returnStatus, rows) {
    console.log('here');
  });

  request.on('requestCompleted', (rowCount, more) => {
    connection.close();
  });

  connection.execSql(request);

}

Hope this helps!

@mShan0 This fixed everything, thank you!