在节点发布请求中的csv文件中追加

时间:2019-04-03 12:33:03

标签: node.js csv

我正在将一个csv文件上传到使用fast-csv进行验证的服务器并将其保存到db,当文件接收到我想在其中添加更多行时,我使用csv-write-stream进行追加。

   const fs = require('fs');
   var csvWriter = require('csv-write-stream');
   var writer = csvWriter({sendHeaders: false});
   var multer = require('multer');
   var csv = require('fast-csv');
   var storage = multer.memoryStorage();
   var upload = multer({storage: storage}).single('file'); 


exports.uploadContact =(req, res) => {

    upload(req, res, function (err) {

         writer = csvWriter({sendHeaders: false});  

         writer.pipe(fs.createWriteStream(req.file.originalname, {flags: 'a'})); 
         writer.write({first_name: "world"}); 

       //here i am tring to append "world" below the header 
       // "first_name" but it does not append on the existing file 
       // but creates new one

         writer.end();

    csv.fromString(req.file.buffer.toString(), {
              headers: true, 
              ignoreEmpty: true,

          })
             .on("data", function (data) {});

               //here new rows are not present

            });
           });

    }

0 个答案:

没有答案