Arty
Arty

Reputation: 355

SQL Server Tedious Callback

I can't use the callback of tedious on Request (This is a INSERT request) to make another request just after the first one. Any idea ? Here is my code.

function intermediaryPositionSQL(decodeMessage, connection) {
  if (typeof (decodeMessage.latitudeInt) != "undefined" && typeof (decodeMessage.longitudeInt) != "undefined")
  {
    var request = new Request(requestPosQuery, function (error)
    {
      if (error) {
        winston.error(error);
      }
    });
    request.addParameter('v_pos_latitude', TYPES.Float, decodeMessage.latitudeInt);
    request.addParameter('v_pos_longitude', TYPES.Float, decodeMessage.longitudeInt);
    request.addParameter('v_pos_altitude', TYPES.Int, (typeof (decodeMessage.altitude) != "undefined") ? decodeMessage.altitude : null);
    request.addParameter('v_pos_speed', TYPES.Int, (typeof (decodeMessage.speed) != "undefined") ? decodeMessage.speed : null);
    request.addParameter('v_pos_move', TYPES.Int, decodeMessage.move);
    request.addParameter('v_pos_type', TYPES.Int, 2);
    request.addParameter('v_pos_device', TYPES.VarChar, decodeMessage.device);
    request.addParameter('v_pos_timestamp', TYPES.Int, decodeMessage.time);

    request.on('doneInProc', function (rowCount, more, rows) {
        return;
    });

    connection.execSql(request);
  }  else {
    return;
  }
}

function actualPositionSQL(decodeMessage, connection) {
  if (typeof (decodeMessage.latitude) != "undefined" && typeof (decodeMessage.longitude) != "undefined")
  {
    var request = new Request(requestPosQuery, function (error)
    {
      if (error) {
        winston.error(error);
      }
    });
    request.addParameter('v_pos_latitude', TYPES.Float, decodeMessage.latitude);
    request.addParameter('v_pos_longitude', TYPES.Float, decodeMessage.longitude);
    request.addParameter('v_pos_altitude', TYPES.Int, (typeof (decodeMessage.altitude) != "undefined") ? decodeMessage.altitude : null);
    request.addParameter('v_pos_speed', TYPES.Int, (typeof (decodeMessage.speed) != "undefined") ? decodeMessage.speed : null);
    request.addParameter('v_pos_move', TYPES.Int, decodeMessage.move);
    request.addParameter('v_pos_type', TYPES.Int, 1);
    request.addParameter('v_pos_device', TYPES.VarChar, decodeMessage.device);
    request.addParameter('v_pos_timestamp', TYPES.Int, decodeMessage.time);

    request.on('doneInProc', function (rowCount, more, rows) {
        intermediaryPositionSQL(decodeMessage, connection);
    });

    connection.execSql(request);
  }  else {
    intermediaryPositionSQL(decodeMessage, connection);
  }
}

This code should just INSERT a first time with a SQL request and then do it again with new values

The error is :

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

Here is the configuration of my Azure SQL Server :

var config = {
  userName: '***@***',
  password: '*******',
  server: '******.database.windows.net',
  options: {encrypt: true, database: '******'}
};

I tried with doneProc, row and even with done without success. doneProc makes the same error as doneInProc and row / done are not called.

Upvotes: 2

Views: 1598

Answers (1)

walley
walley

Reputation: 135

Try using the callback in the Request constructor to call your next query:

function firstQuery(param, connection) {
    if (typeof (param.foo) != "undefined" && typeof (param.bar) != "undefined") {
       var request = new Request(firstSQL, function (error, rowCount) {
          if (error) {
             winston.error(error);
          }
          else {
             secondQuery(param, connection);
          }
       });
       // add parameters
       connection.execSql(request)
    }
}

according to the documentation this callback is called when the request is complete. The doneInProc and doneProc events are only relevant when you're using "stored procedures" on your database server. For a simple "insert" or "select" statement the Constructor callback function is what you want to be using.

Upvotes: 2

Related Questions