'Done' event not firing when request is complete
Per the title, the 'done' event is not being fired when the request returns all rows. I have something like the following, which is retrieving row data:
request = new Request(statement, function (err, rowCount) {
// Check for errors etc.
connection.close();
});
request.on("row", function (fields) {
// Do stuff with row data
});
request.on("done", function (rowCount, more) {
console.log(rowCount + " rows returned."); // <-- This is never executed.
});
More details available upon request, thanks.
Matt
I've been facing this issue too. I was able to work around it by putting the code in the Request constructor callback. But yeah, a bug is a bug.
What function are you using on the Connection? execSql, execSqlBatch or execute? They can each result in different permutations of done, doneInProc and doneProc being emitted, depending on the statements executed. It's entirely possible that doneProc is being emitted, even if you are not executing a procedure.
The done... events are relatively low level, and are driven by TDS tokens received. In most cases relying on the Request's callback to be notified of completion will be the best approach. I should probably make this clearer in the documentation.
@pekim thanks for the clarification
Having similar issues with the done event not firing while using execute.
For what it's worth, I'm connecting to SQL Server 2012. A quick google search doesn't really give me an insight as to whether or not the TDS Protocol version changed for 2012. I'm using the callback to close the connection and return data without issue, though.
Is the doneProc event firing instead?
@pekim sorry for not clarifying earlier, but I was executing queries using the execsql function after making a new request object using tedius.Request.
@pekim I am experiencing the same issue when done is not emitted when executing connection.execSql(request) using node 0.8.18. FWIW, while tracing the code, L355 of connection.js never evaluates to true but is executed twice. Is _this.request ever assigned a value?
doneProc is being emitted.
@pekim doneProc is indeed firing but returning an undefined rowcount. However, doneInProc is also firing and actually returning a rowcount value.
I have the same problem. You should definitely update the documentation and maybe even change the event names or something or even omit them completely as they are really misleading. I wasted some time figuring the problem out.
@pekim thanks for clarifying. Really appreciate the work you put on this plugin :) Let me know if you need any help
I'm using execSql and there is nothing comes out when listening to 'done', while there is outcome of console.log but no rowCount in event of 'doneProc', and console.log in conjunction with the correct rowCount in event of 'doneInProc' !!
same problem here, i'm using execSql, although doneProc and doneInProc get triggered i'm not getting any data from rows even when rowsCount is greater that 0 (in my case only one row)
@deanvlue I really can not reproduce this issue. What version of SQL Server are your running against? Which version of tedious? Can you come up with a minimal reproducible test case?
I believe that people misunderstand how and when the done, doneProc and doneInProc events fire, so understanding your issues will allow me to clarify the docs.
I have exactlty the same issue.
connection.on('connect', function(err) {
// If no error, then good to proceed.
if (err) return console.error(err);
else console.log("Connected");
var rq = new TRequest("SELECT * FROM mytable;", function(err, rowCount) {
if (err) {
console.log(err);}
console.log("Row Count: " + rowCount);
});
var result = "";
rq.on('row', function(columns) {
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
result+= column.value + " ";
}
});
console.log(result);
result ="";
});
rq.on('doneProc', function(rowCount, more) {
console.log("lallalla");
console.log(rowCount + ' rows returned');
});
connection.execSql(rq);
});
The done event cannot be fired in execSql, doneProc could be fired, however, rowCount returns as undefined.
I am on the latest Azure V12 sql server DB.
I'm seeing the same issue. For my execSql request:
donedoesn't fire,doneProcfires butrowCountis returned as undefined,doneInProcfires first with correctrowCount.
Is that expected? Should we be updating the docs to reflect this? @arthurschreiber @pekim, can you please help confirm this?
Repro code:
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
// Create connection to database
var config = {
userName: 'mylogin',
password: 'mypassword',
server: 'localhost',
options: {
database: 'testdb'
}
}
var connection = new Connection(config);
// Attempt to connect and execute queries if connection goes through
connection.on('connect', function(err) {
if (err) {
console.log(err);
return;
}
console.log("Reading rows from the Table...");
// Read all rows from table
request = new Request("SELECT * FROM dbo.employees;", function(err) {
if (err) {
console.log(err);
}
});
// Print the rows read
var result = "";
request.on('row', function(columns) {
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
result+= column.value + " ";
}
});
console.log(result);
result ="";
});
// Output the number of rows read
request.on('done', function (rowCount, more, rows) {
console.log('done: '+ rowCount + ' row(s) returned');
});
request.on('doneProc', function (rowCount, more, returnStatus, rows) {
console.log('doneProc: '+ rowCount + ' row(s) returned');
});
request.on('doneInProc', function (rowCount, more, rows) {
console.log('doneInProc: '+ rowCount + ' row(s) returned');
});
connection.execSql(request);
});
Here's the output:
Reading rows from the Table...
2 Sunil India
3 Tom Germany
4 Jake United States
doneInProc: 3 row(s) returned
doneProc: undefined row(s) returned
There is a 'callback' property you can set on Request and there is a 'requestCompleted' event on Request, both seem work. And those are the two things we didn't try :)
There may be some cleanup and documentation needed on this.
Try these and see if it works.
Using 'callback':
request.callback = function (err, rowCount, rows) {
// rows is not being set but rowCount is. May be a bug.
if (err) {
// Error handling.
} else {
// Next SQL statement.
}
};
Using 'requestCompleted':
request.on('requestCompleted', function () {
if (runAgain) {
// Next SQL statement.
}
});
@tvrprasad - Not sure I understand the response. All I'm saying is that done, doneProc and doneInProc are behaving differently than stated in the documentation :-)
oops. I mixed up issues there :-( Sorry about that. Ignore my comment please.
No worries! @tvrprasad - do you have permissions to fix the documentation?
@arthurschreiber can we please update the docs for done, doneProc and doneInProc. They are behaving differently than stated in the documentation