SQL Server枯燥的callback

我不能使用请求(这是一个INSERT请求)冗长的繁琐的callback在第一个请求之后发出另一个请求。 任何想法 ? 这是我的代码。

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); } } 

这段代码应该首先插入一个SQL请求,然后再用新的值重新执行

错误是:

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

这里是我的Azure SQL Server的configuration:

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

我尝试了doneProc,行,甚至没有成功。 doneProc与doInProc产生相同的错误,row / done不会被调用。

尝试在Request构造函数中使用callback来调用您的下一个查询:

 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) } } 

根据文档,这个callback在请求完成时被调用。 doneInProcdoneProc事件只有在数据库服务器上使用“存储过程”时才相关。 对于一个简单的“插入”或“select”语句,构造函数callback函数就是你想要使用的。