简体   繁体   English

将异步数据库的数据导出到csv文件中

[英]Export asynchronous database's data into csv file

I'm currently trying to write results of a sql query into a CSV file using csv-stringify module for NodeJs.我目前正在尝试使用 NodeJs 的csv-stringify模块将 sql 查询的结果写入 CSV 文件。

Here is my code :这是我的代码:

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

The variable results.recordset looks like that :变量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
              }
            ]

This piece of code return the following error.这段代码返回以下错误。

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'

I guess there is this error because it's asynchrone and the results variable is undefined when I use the stringify() function.我猜有这个错误是因为它是异步的,并且当我使用stringify()函数时results变量未定义。 But I don't know how to rewrite my code so it can work fine.但我不知道如何重写我的代码才能正常工作。

Results variable will not be undefined because you are using async function and stringify will call only after you get results.结果变量不会未定义,因为您使用的是异步函数,而 stringify 只会在您获得结果后调用。 Could you please add how the results variable look for you before you call stringify ?您能否在调用 stringify 之前添加结果变量如何查找您?

In additional I would recommend you to define columns option此外,我建议您定义列选项

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM