2

嗨,我是单调乏味和 Es-6 的新手,这可能是一个愚蠢的问题,但我很挣扎,我想要数组中的总数据,这里使用单调乏味的连接是我的代码:

 getZipData() {
        var Connection = require('tedious').Connection;
        Request = require('tedious').Request;
        var config = {
            userName: 'xx',
            password: 'xxxx',
            server: 'xxx', // You can use 'localhost\\instance' to connect to named instance
            options: {
                database: 'xxxxx',
                rowCollectionOnDone:'true'
            }
        }
        var connection = new Connection(config);
        var jsonArray = [];
        connection.on('connect', function (err) {
            if (err) {
                console.log(err)
            }

            var sql = "SELECT * FROM xxxxx";
            return new Promise(function(resolve,reject){
            var request = new Request(sql,
                (err, rowCount, rows)=>{
                    if (err) {
                        reject(err);
                    }
                    else {
                        alert("rows");
                        console.log(rowCount + 'rows');
                    }
                });
                request.on('row', (columns)=>{
                        var rowObject = {};
                        columns.forEach((column)=> {
                            rowObject[column.metadata.colName] = column.value;
                        });
                        jsonArray.push(rowObject);
                    });
                connection.execSql(request);
                request.on('done', function(rowCount, more) {
                    console.log(rowCount + ' rows returned');
                    alert("jsonArray2:"+jsonArray);
                    resolve(jsonArray)
                });

            });
    })
}
componentWillMount() {
    this.getZipData().then(function(resolved){
        console.log(resolved);
        alert("data:"+resolved);
    }).catch(function(rejected){
        console.log(rejected);
    })
}

当我添加 request.on('done', function(rowCount, more) 时,我也没有得到任何数据,谁能给出解决方案,我希望显示总数据

4

1 回答 1

0

看起来您在执行查询之前调用了 resolve:

var jsonArray = [];

// Register callback for row event
request.on('row', (columns)=>{
  var rowObject = {};
  columns.forEach((column)=> {
    rowObject[column.metadata.colName] = column.value;
  });

  jsonArray.push(rowObject);
});

// Call resolve before executing request
resolve(jsonArray);

connection.execSql(request);

文档提到了一个done表明请求已完成的事件:

request.on('done', function (rowCount, more, rows) {
  // Call resolve here instead?
  resolve(jsonArray);
});

免责声明:我实际上并没有使用过 Tedious,但是从链接的文档来看,这看起来像您正在寻找的东西。

于 2015-08-10T09:38:17.130 回答