【问题标题】:My npm sql query results are formatted in JSON but I need them to be stringify我的 npm sql 查询结果采用 JSON 格式,但我需要将它们字符串化
【发布时间】:2017-04-14 20:42:07
【问题描述】:

这是一个连接到 SQL 数据库并对 EMPLOYEE 表进行 SQL 选择查询的简单示例。在 npm 2.1.8 中,结果以字符串化格式返回,但现在它在每条记录的多行中返回。我已经提供了我正在使用的代码和一个结果示例,以及一个我希望如何返回结果的示例。

const sql = require('mssql')
//const sql = require('mssql/msnodesqlv8')

const dbConfig = {
    user: 'sa',
    password: 'password',
    server: 'localhost',
    database: 'LWWEBAPP'
};

function getEmp() {
    const conn = new sql.ConnectionPool(dbConfig);
    //var req = new sql.Request(conn);

    sql.connect(dbConfig, err => {
          if (err) {
            console.log(err);
            return;
          }

        const request = new sql.Request()
        request.stream = true
        request.query('select * FROM EMPLOYEE') 

        request.on('recordset', columns => {
            //console.log(columns) 
        })

        request.on('row', row => {
            console.log(row) 
        })

        request.on('error', err => {
            if (err) {
                console.log(err);
                return;
            }
        })

        request.on('done', result => {
            //console.log(result) 
        })
    })

    sql.on('error', err => {
        if (err) {
            console.log(err);
            return;
        }
    })
}
getEmp();

这是我得到的结果:

  {    EMPLOYEE_NUMBER: '100',
       FIRST_NAME: 'Jim',
       LAST_NAME: Smith, }
  {    EMPLOYEE_NUMBER: '101',
       FIRST_NAME: 'John',
       LAST_NAME: Smith }
  {    EMPLOYEE_NUMBER: '102',
       FIRST_NAME: 'Sue',
       LAST_NAME: Smith }

我希望每条记录在一行中显示结果:

[ {EMPLOYEE_NUMBER: '100', FIRST_NAME: 'Jim', LAST_NAME: 'Smith'} .
  {EMPLOYEE_NUMBER: '101', FIRST_NAME: 'John', LAST_NAME: 'Smith'} .
  {EMPLOYEE_NUMBER: '102', FIRST_NAME: 'Sue', LAST_NAME: 'Smith'} ]

【问题讨论】:

    标签: sql json node.js


    【解决方案1】:

    尝试:

    request.on('row', row => {
        console.log(JSON.stringify);
    })
    

    你会得到这样的结果,但没有数组闭包

    var rows = [{
        EMPLOYEE_NUMBER: '100',
        FIRST_NAME: 'Jim',
        LAST_NAME: "Smith",
      },
      {
        EMPLOYEE_NUMBER: '101',
        FIRST_NAME: 'John',
        LAST_NAME: "Smith"
      },
      {
        EMPLOYEE_NUMBER: '102',
        FIRST_NAME: 'Sue',
        LAST_NAME: "Smith"
      }
    ]
    
    rows.forEach(function(elem) {
      alert(JSON.stringify(elem));//{"EMPLOYEE_NUMBER":"100","FIRST_NAME":"Jim","LAST_NAME":"Smith"}
    });
    
    alert(JSON.stringify(rows));

    如果您想查看数组,您必须将所有结果行收集到数组中,然后调用JSON.stringify(yourArray)

    【讨论】:

    • Pavlo,如果你能提供更清楚一点的话。我很难让这个工作。这是我将 console.log(row) 替换为 console.log(JSON.stringify) [Function: stringify] [Function: stringify] [Function: stringify] [Function: stringify] [Function: stringify]
    • @joey.coyle 你有错误的替换。右边是console.log(JSON.stringify(row));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多