NodeJS MSSQL .query在记录集和记录集中都返回双精度数据 [英] NodeJS MSSQL .query returning double data in both recordsets and recordset

查看:411
本文介绍了NodeJS MSSQL .query在记录集和记录集中都返回双精度数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到从Node中的mssql返回的JSON中所有数据行重复了两次:

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

}

在返回JSON的app.get语句中调用了上述函数.

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

console.dir(result)显示与上面的JSON相同,除了在第一行显示"[Object]:.所以我认为它不会进一步包装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.

推荐答案

数据不会重调两次,而只是通过两个属性公开. recordset属性仅公开recordsets中的第一个记录集.

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

mssql文档:

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

这篇关于NodeJS MSSQL .query在记录集和记录集中都返回双精度数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆