Tuesday 6 June 2017

Can't set headers after they are sent - routing

I have seen multiple answers for the question but I am not able to solve this issue, so I am re-posting it with my code. I am new to this and need help to understand and fix this.

My Code

// Get logs from DB
router.get("/getlogs/:dbtype", function (req, res, next) {
var dbtype = req.params.dbtype;
if (dbtype == "mongodb") {
    list = Logs.find(function (err, log) {
        res.json(log);
    })
} else if (dbtype == "mssql") {
    var config = {
        userName: 'user',
        password: 'pass',
        server: 'server',
        options: {
            instanceName: 'instance',
            database: 'db',
        }
    };

    // SQL Server connection
    var connection = new Tedious.Connection(config);
    connection.on('connect', function (err) {
       // if (err) { console.log(err); res.json(err);}
        // If no error, then good to proceed.  
        //console.log("SQL Server Connected SQL SQL");
        //connection.on('debug', function(err) { console.log('debug:', err);});

        var request = new Tedious.Request(`select top 1 * from table1`, function (err) {
            //if (err)  { console.log(err); res.json(err);}
        });

        request.on('row', function (columns) {
            var row = {};
            columns.forEach(function (column) {
                row[column.metadata.colName] = column.value;
            });
            rows.push(row);
            res.json(rows); **<-- this is where I get error**
        });

        connection.execSql(request);
    });
}
});



via Rohit

No comments:

Post a Comment