Re: [nodejs] Tedious - Problem in sending response

1,710 views
Skip to first unread message

Murvin Lai

unread,
Jan 9, 2013, 2:52:55 PM1/9/13
to nod...@googlegroups.com
What i think what happened is that, before done, something already fired res.send or res.end, so, when request.on('done') is executed, res is no longer valid for send again.

possibly,...   connection.execSql(request);   send something out.  


On Tue, Jan 8, 2013 at 11:37 PM, Bhoomi Kakaiya <bhoomi....@gmail.com> wrote:

Hi all,

I am pretty new to Node. I am using Tediuos to get data from SQL database. 

Here is the code snippet :

app.get('/getitems', function(req, res){
res.contentType('application/json');
var config = { userName: 'sa', password: 'pass', server: '127.0.0.1', options : { database:'Auto'}
 };
var connection = new Connection(config);
connection.on('connect', function(err) {
executeStatement(res);
 });
function executeStatement(res1) {
     console.log(res1.contentType);
 request = new Request("select top 3 sItemId [Id], sName [Item], nrate1 [Rate] from bmitem", function(err, rowCount) {
if (err) {
 console.log(err);
} else {
 console.log(rowCount + ' rows');
}
connection.close();
 });

 request.on('row', function(columns) {
columns.forEach(function(column) {
 if (column.value === null) {
console.log('NULL');
 } else {
console.log(column.value);
 }
});
 });
 request.on('done', function(rowCount, more) {
console.log(rowCount + ' rows returned');
debugger;
res1.send("3");// Here is the problem. Debugger shows res1 is <error>.
 });
 connection.execSql(request);
}
});


The problem is response object has error.
I want to create json of the items returned from db and want to pass it as json in the ajax request.

If I return response in request.on("row") it works. Do not understand why response object is not available on "done" event.

Thanks in advance.

Bhoomi.


--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Andrew Maurer

unread,
Jan 9, 2013, 6:22:09 PM1/9/13
to nod...@googlegroups.com
You need to res.send() in the new Request() constructor. I know, it isn't clear that the callback is actually what happens when the request is complete. 

So before "connection.close();" send your payload. In your "row" event, collect your results. Here is some sample code I wrote a few days ago (ironically, I too just picked up Tedious.) 

var results = [];

var conn = new Connection(connectionObject);
conn.on("connect", function(){
var r = new Request("exec andrew.dbo.usp_getLogEntriesByDay", function(){
res.set({
'Expires' : new Date(Date.now() + 900000)
})
res.json(results);
});
r.on("row",function(row){
results.push({
countof : Number(row[0].value),
createdDateTime : new Date(row[1].value).getTime()
});
});
this.execSql(r);
});

Bhoomi Kakaiya

unread,
Jan 10, 2013, 12:17:19 AM1/10/13
to nod...@googlegroups.com
That is it!

Thank you so much..



Mike Pilsbury

unread,
Jan 10, 2013, 3:08:00 PM1/10/13
to nod...@googlegroups.com
Boomi, I really sorry that I didn't answer this question when you posted it in the tedious group. I simply haven't had any proper spare time the last few days.

I'm pleased that you got a good answer; better than one I might have provided. 

Mike

Mike Pilsbury

unread,
Mar 23, 2013, 11:47:31 AM3/23/13
to nod...@googlegroups.com
Like most io in node, tedious responds asynchronously. However you are treating your connect_db function as if it operated synchronously. The result array will still be empty when the function returns.

Pushing stuff in to the result array in the 'row' event is fine; that will allow you to accumulate the data.

You possibly only want to do
res.write(results);
res.end("hello world");
in the Request's callback function (which is called once the request is complete).

So something along these lines.
if (err) {
...
} else {
res.write(results);
res.end("hello world");
}
Of course you'll need to pass res to your connect_db function.

Or you could write each result in the 'row' event handler, then
res.end("hello world");
in the Request's completion function.


On Friday, 22 March 2013 17:06:23 UTC, Ibrahim Gaal wrote:
Hi Guys,

I'm also very new to node, and would appreciate any help you can provide.
I have a http webserver created in node js. once you hit the server I'd like it respond with some json data.
I'm able to connect to my sql server database using the tedious module. I can extract data from my tables and write to console, however I'm having problems returning the data back as the http web server's response.

please see code below 

var http = require('http');
var s = http.createServer(function (req, res) { 
    res.writeHead(200, {'Content-Type': 'text/plain'}); 
    
var results = connect_db();
res.write(results);
res.end("hello world");


}); 
s.listen(9090);


function connect_db(){

// Global variables 
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;

var result = []; 
var config = {
userName: 'user1',
password: 'password',
server: 'localhost',
"options": {
"database": "testdb",
}
};

// create connection instance/object  
var connection = new Connection(config);

connection.on('connect', function(err){

if (err){
console.log(err);
console.log('Unable to connect to database');
else {
console.log('connection to database established');
}

// create Request instance
request = new Request("select top 1 id from table1", function(err, rowCount) {
// error check request
if (err) {
console.log(err);
console.log('request error');
}

// close connection after rows return for request
if (rowCount)
{
console.log('finished');
connection.close();
console.log('db connection closed');
}

});


request.on('row', function(columns) {
columns.forEach(function(column) {
//console.log(column.value);
results.push(column.value);
//console.log(foo);

});
});

connection.execSql(request);

});


return result;
}

Ibrahim Gaal

unread,
Mar 25, 2013, 12:58:20 PM3/25/13
to nod...@googlegroups.com
Thanks Mike, appreciate the help :)
Reply all
Reply to author
Forward
0 new messages