Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS MSSQL .query returning double data in both recordsets and recordset

I'm seeing all data rows repeated twice in my JSON returned from mssql in Node:

{
        "recordsets": [[{
                    "student_firstname": "Jonah                ",
                    "student_lastname": "Hill                    "
                }, {
                    "student_firstname": "Debra                   ",
                    "student_lastname": "Smith               "
                }
            ]],
        "recordset": [{
                "student_firstname": "Jonah                ",
                "student_lastname": "Hill                    "
            }, {
                "student_firstname": "Debra                   ",
                "student_lastname": "Smith               "
            }
        ],
        "output": {},
        "rowsAffected": [2]
    }

I temporarily changed the query to get two rows to see if all rows would be duplicate, and they are as you can see above.

function getStudent(studentID) 
{
    console.log("---------getStudent"); 


    sql.on('error', err => {
        // ... error handler 
        console.log("DB Error2: " + err); 
    })


    return sql.connect(config).then(pool => {
            // Query 
            return pool.request()
            .input('input_parameter', sql.Int, studentID)
            //.query('select student_firstname, student_lastname from students where student_id = @input_parameter')
            .query('select student_firstname, student_lastname from students where student_id in (31,32)')
        }).then(function(result) {
            console.log("getStudent:then(result=>"); 
            console.dir(result);
            sql.close(); 
            return result; 
        })
        .catch(err => {
            // ... error checks 
            console.log("DB Error1: " + err); 
            sql.close(); 
            throw err; 
        })

}

The above function is called in an app.get statement that returns the JSON.

The console.dir(result) shows the same as the JSON above, except shows "[Object]: in the first line. SO I don't think it's wrapping the JSON further.

{ recordsets: [ [ [Object], [Object] ] ],
  recordset:
   [ { student_firstname: 'Jonah                  ',
       student_lastname: 'Hill                    ' },
     { student_firstname: 'Debra                   ',
       student_lastname: 'Smith                   ' } ],
  output: {},
  rowsAffected: [ 2 ] }

I can work with the data like this, but it's wasting bandwidth.

like image 500
NealWalters Avatar asked Aug 02 '17 14:08

NealWalters


1 Answers

The data is not retuned twice but just exposed through two properties. The recordset property just exposes the first recordset in recordsets.

mssql documentation:

result.recordsets.length // count of recordsets returned by the procedure
result.recordsets[0].length // count of rows contained in first recordset
result.recordset // first recordset from result.recordsets
like image 121
Timo Avatar answered Nov 15 '22 17:11

Timo