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);
});
On Wednesday, January 9, 2013 2:37:13 AM UTC-5, Bhoomi Kakaiya 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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en