【问题标题】:Export asynchronous database's data into csv file将异步数据库的数据导出到 csv 文件中
【发布时间】:2021-01-09 18:31:57
【问题描述】:

我目前正在尝试使用 NodeJs 的 csv-stringify 模块将 sql 查询的结果写入 CSV 文件。

这是我的代码:

async exportData() {
        try {
            let request = 'SELECT * FROM PROFACE.dbo.EvenementsMachines WHERE idMachine=215';
            let results = await sql.query(request);

            stringify(results.recordset, {
                    header: true
            }, function (err, output) {
                    fs.writeFile(__dirname+'/results.csv', output, function(err, result) {
                        if (err) {
                            console.log('error : ', err);
                        }
                    });
            })
        } catch (e) {
            console.log(e);
        }
    }

server.app.get("/export", function(req, res) {
    try {
        exportData().then(function(value) {
            res.json("OK");
        });
    } catch (e) {
        console.log(e);
    } 
})

变量results.recordset 看起来像这样:

            [
              {
                timeStamp: '2020-09-22T05:00:08.230Z',
                compteurTotalPieces: 3899,
                compteurTotalNC: 380
              },
              {
                timeStamp: '2020-09-22T05:02:08.223Z',
                compteurTotalPieces: 21,
                compteurTotalNC: 2
              },
              {
                timeStamp: '2020-09-22T05:04:08.233Z',
                compteurTotalPieces: 43,
                compteurTotalNC: 3
              },
              {
                timeStamp:' 2020-09-22T05:06:08.296Z',
                compteurTotalPieces: 66,
                compteurTotalNC: 4
              },
              {
                timeStamp: '2020-09-22T05:08:08.370Z',
                compteurTotalPieces: 88,
                compteurTotalNC: 5
              },
              {
                timeStamp: '2020-09-22T05:10:08.423Z',
                compteurTotalPieces: 110,
                compteurTotalNC: 6
              },
              {
                timeStamp: '2020-09-22T05:12:08.490Z',
                compteurTotalPieces: 133,
                compteurTotalNC: 7
              },
              {
                timeStamp: '2020-09-22T05:14:08.520Z',
                compteurTotalPieces: 156,
                compteurTotalNC: 9
              }
            ]

这段代码返回以下错误。

CsvError: Invalid argument: got {"header":true} at index 1
    at stringify (C:\Users\c.chaulaic\Documents\Dev\mps-prod\node_modules\csv-stringify\lib\index.js:482:13)
    at DAO.exportData (C:\Users\c.chaulaic\Documents\Dev\mps-prod\server\DAO.js:219:4)
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  code: 'CSV_INVALID_ARGUMENT'

我猜有这个错误,因为它是异步的,当我使用 stringify() 函数时,results 变量未定义。但我不知道如何重写我的代码以便它可以正常工作。

【问题讨论】:

    标签: javascript node.js csv asynchronous fs


    【解决方案1】:

    Results 变量不会未定义,因为您使用的是异步函数,并且 stringify 只会在您获得结果后调用。您能否在调用 stringify 之前添加结果变量的外观?

    另外,我建议您定义列选项

    stringify([{name: '1'}, {name: '2'}], {
        header: true,
        columns: ['name']
      }, function (err, output) {
      });
    

    【讨论】:

    • 但我只是执行你的代码,它对我有用:)
    猜你喜欢
    • 2013-03-19
    • 2013-09-10
    • 2014-11-05
    • 1970-01-01
    • 2012-12-22
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多