Question

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);
    }
});
Was it helpful?

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
    })
);

OTHER TIPS

Regarding the code:

  var request = new Request("SELECT * FROM Products", function(err, rowCount, rows) {
      console.log(rowCount);
      res.send(rows);
  })

The problem occurs because rows is undefined. This callback only receives 2 parameters:

  var request = new Request("SELECT * FROM Products", function(err, rowCount) {
      console.log(rowCount);
      res.send(rows);
  })

I had to use this config to make it work

    var dbConn = await this.db.connect(); // Here add your connection code in connect() function
    const allRows = [];
    return await new Promise((resolve,reject) => {
       var SELECT_QUERY = 'SELECT * FROM your_table ';
       const request = new Request(SELECT_QUERY, function(err, rowCount) {
            if (err) {
                return reject(err);
            } else {
                console.log(rowCount + ' rows');
            }
        });

        request.on('row', function(columns) {

            columns.forEach(function(column) {
                const row = [];
                row.push({
                    metadata: column.metadata,
                    value: column.value,
                    toString: () => column.value
                });
                allRows.push(row);
            });
        });

        request.on('doneProc', function (rowCount, more, returnStatus, rows) {
            console.log('onDoneProc');

            console.log('all rows',allRows);

            return resolve(allRows);
        });

        dbConn.execSql(request);

    });

as per the Tedious API

    callback
    function (err, rowCount, rows) { }

rows Rows as a result of executing the SQL statement.

Will only be available if Connection's config.options.rowCollectionOnRequestCompletion is true.

I had to use this config to make it work

     var config = {
    server: "localhost",
    database: "*****",
    userName: "sa",
    password: "******",
    port: 1433,
    options:{rowCollectionOnRequestCompletion:true}
};

This is just a wild guess, but looking at the source, the rows get assigned an empty array when the procDone event is called. Perhaps, try setting rowCollectionOnDone to false? Also, there seems to be a row event emitted on the request. You could also subscribe to that and see if that gets you any output.

set "rowCollectionOnRequestCompletion" is true

var config = {
    ...
    options : {
        rowCollectionOnRequestCompletion : true   // add this line
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top