0

I have used tedious to connect to sql server and restify for restful api here is the server.js

server.get('/getInvoiceData', function (req, res, next) {
    repository.GetInvoiceData(function(data){
        res.send(data);
        next();
    });
});

and the invoice.js

exports.GetInvoiceData = function(callback){
    var query = "SELECT * FROM [Snapdeal].[dbo].[tbl_Configuration]";
    var req = new request(query,function(err,rowcount){
        if (err)
        {
            console.log(err.toString());
        }else{
            console.log(rowcount+ " rows");
        }
    });
    req.on('row',function(){
        callback({customernumber:123});
    });
    connection.execSql(req);
}

I am getting the error as Cant set the headers after they are sent.

Prateek Dhuper
  • 235
  • 4
  • 14

2 Answers2

3

I am not 100% sure as I am not familiar with the SQL lib you are using, however, it looks to me like the problem is your row event would be raised per row, rather than per transaction.

You are ending the response after the first row event therefore if there is more than one row being returned the response will already have been closed (hence the error).

One way of dealing with this is to accumulate the row data as it's being retrieved and then raise the callback after your done


Now that you have stated the lib you are using (Tedius), it would appear my hunch was correct. Looking at the library, here is the simplest approach you can take to returning all the rows in a single callback

exports.GetInvoiceData = function(callback){
    var query = "SELECT * FROM [Snapdeal].[dbo].[tbl_Configuration]";
    var req = new request(query,function(err, rowcount, rows){
        if (err) {
            console.log(err.toString());
        } else{
            callback(rows);
        }
    });
    connection.execSql(req);
}

Note - remember to set config.options.rowCollectionOnRequestCompletion to true otherwise the rows parameter will be empty.

James
  • 80,725
  • 18
  • 167
  • 237
  • still it just printing the no, of rows on console , but the browser doesn't get any output. Yes ,but the error is resolved and now y am i not getting the output on browser – Prateek Dhuper Feb 06 '15 at 13:54
  • @PrateekDhuper you are using the updated example? According to the docs the `rows` parameter returns the row data so you don't need to explicitly listen for the `row` event. – James Feb 06 '15 at 13:56
  • @James Using this update example of yours i m getting rowcount as 145 but the rows =[] – Prateek Dhuper Feb 06 '15 at 13:59
  • @PrateekDhuper just re-read the [docs](http://pekim.github.io/tedious/api-request.html), you need to have the `config.options.rowCollectionOnRequestCompletion` option set to `true` for the `rows` parameter to become available. I have added a note to my answer. – James Feb 06 '15 at 14:01
0

My issue, using mssql, was that I had a default value or binding set (in this case, (getdate())) to one of my columns (modified date column). However, the data I was trying to retrieve had preset NULL values for this particular column.

I put data in those rows and I was good to go.

digiwand
  • 1,258
  • 1
  • 12
  • 18