Search code examples
sql-servernode.jsexpresstedious

Node.js Tedious Rows is an empty array but RowCount is correct


So this is my basic chunk of code that I'm running. I've got RowCount coming back as expected (there's only 5 items right now) and but rows comes back as an empty array.

Am I doing something wrong? By the way I'm connecting to SQL azure. I don't have any connection problems and I do believe that I've put the correct options (rowCollectionOnRequestCompletion to true).

Any ideas?

var Connection = require('tedious').Connection;
var Request = require('tedious').Request;

exports.list = function(req, res){
    var connection = new Connection({
    "userName": "myCoolUsername",
    "password": "SoMePa$$word",
    "server": "something.database.windows.net",
    "options": {
        "database": "mySampleDbName",
        "encrypt": true,
        "rowCollectionOnDone": true,
        "rowCollectionOnRequestCompletion": true
    }
});

connection.on('connect', function(err){
    //if no error, then we are good to go.
    if(err){
        console.log(err);
    }else
    {
        var request = new Request("SELECT * FROM Products", function(err, rowCount, rows){
            console.log(rowCount);
            res.send(rows);
        })
        connection.execSql(request);
    }
});

Solution

  • I had the same problem. Solved using rowCollectionOnDone: true option and doneInProc event on Request object like below. I don't know why callback function return empty array, when it should.

    var config = {
        userName: '...',
        password: '...',
        server: 'localhost',
        options: {
            port: 2005,
            database: 'db1',
            rowCollectionOnDone: true
        }
    }
    
    connection.execSql(new Request('SELECT * FROM Products', function(err, rowCount, rows){
            if(err) {
                throw err;
            }
        })
        .on('doneInProc',function(rowCount, more, rows){
            console.log(rows); // not empty
        })
    );