使用Node.js在for循环中将数据写入多个文件

时间:2018-08-08 12:02:36

标签: javascript node.js csv

在我当前的项目中,我正在通过API读取数据,我想为每个设备(我有大约100个设备)有多个文件,并且对于每个设备,我想创建.csv文件,编写多行到文件(使用节点csv-write-stream包)并关闭文件。同样,我想对每100个设备执行相同的操作。我尝试了以下代码,但出现错误:

  

写完

   var csvWriter = require('csv-write-stream');
   var writer = csvWriter({ headers: ["TimeStamp", "Gas","gw_temp","rh","abrasion","sensor_temp"]});
      for(let j=0;j<data.length;j++){
      // Create file for each devices
       await writer.pipe(fs.createWriteStream(data[j][0] + '.csv',{flags:"a"}));
       // Get data through API
       await axios.get([API CALL URL].join('/'), getConfig)
        .then(async (response) => {
          let res = await response.data;
           if(res.length!=0){
           for (let i = 0; i < res.length; i++) {
              // Write data to csv file
              await  writer.write(res[i]);
           }
          }
        })
        .catch((error) => {
          if (error.response) {
            console.log("Error in response", error.message);
          } else {
            console.log("Other error", error.message);
          }
        });
        await writer.end(); 
        await console.log("File writing done",data[j][0]);
       }
      }

我似乎错过了一件小事,但我无法识别。

我也引用此链接:"Error: write after end" with csv-write-stream

已更新 如果删除catch块,则会引发以下错误:

  

(节点:13264)UnhandledPromiseRejectionWarning:错误:结束后写入       在writeAfterEnd(_stream_writable.js:236:12)       在CsvWriteStream.Writable.write(_stream_writable.js:287:5)

1 个答案:

答案 0 :(得分:0)

最后,我找到了解决方案。我需要每次将writer变量都初始化之前,将数据写入文件,因为它以writer.end()结尾。我正在共享代码:

  // Get sensor measurement data for each devices
  for(let j=0;j<data.length;j++){
  // Intialize the writer everytime before creating a file
    var writer = csvWriter({ headers: ["TimeStamp", "Gas","gw_temp","rh","abrasion","sensor_temp"]});
    await writer.pipe(fs.createWriteStream(data[j][0] + '.csv'));
    await axios.get([API CALL URL].join('/'), getConfig)
    .then(async (response) => {
      let res = await response.data;
      //console.log(res.length);
      if(res.length!=0){
       for (let i = 0; i < res.length; i++) {
         await writer.write(res[i]);
       }
      }
      await console.log("File writing done",data[j][0]);
      await writer.end();

    })
    .catch((error) => {
      if (error.response) {
        console.log("Error in response", error.message);
      } else {
        console.log("Other error", error.message);
      }
    });
  }