【问题标题】:NodeJS MSSQL .query returning double data in both recordsets and recordsetNodeJS MSSQL .query 在记录集和记录集中返回双重数据
【发布时间】:2018-01-09 20:47:44
【问题描述】:

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

{
        "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]
    }

我暂时更改了查询以获取两行,以查看是否所有行都重复,正如您在上面看到的那样。

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 语句中调用。

console.dir(result) 显示与上面的 JSON 相同,除了在第一行显示“[Object]:。所以我认为它不会进一步包装 JSON。

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

我可以处理这样的数据,但它会浪费带宽。

【问题讨论】:

    标签: sql-server json node.js


    【解决方案1】:

    数据不会重新调整两次,而是通过两个属性公开。 recordset 属性只是公开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
    

    【讨论】:

    • 但是数据在浪费字节,因为它在 JSON 中出现了两次。所以这是正常的吗?对我来说似乎很奇怪。
    • @NealWalters,来自这个答案的comment。对象在内存中只有一个引用,即使它出现在两个属性中。我猜数据何时发送回nodejs。只有一组数据。 nodejs 只是将数据转换为两个属性,但引用内存中的同一对象。因此,实际上并没有浪费内存。
    【解决方案2】:

    您可以通过以下查询获取列数据:

    result.recordset[0].student_firstname  // returns the value of the student firstname of first row
    result.recordset[0].student_lastname   // returns the value of the student lastname of first row
    

    【讨论】:

    • 我已经开始做其他事情了;但我认为我的问题的重点是为什么它会返回所有东西的两倍;不是如何访问它。
    【解决方案3】:

    而不是

    console.dir(result);
    

    尝试:

    console.dir(result.recordsets);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 2014-11-06
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多