How to add json data to csv file one by one?

时间:2017-12-17 08:22:36

标签: javascript node.js

I need to start adding json data to csv file from beginning until the end. But each row will be added every minute. In my situation it adds all of them in the first minute. This is my code.

var unirest = require('unirest');
var fs = require('fs');
var outFilename = 'mert_akel_minutereadings.csv';

var interval = setInterval(function () { post(); }, 60000); //Write to file every minute

function post(){
    unirest.post('https://power.ivyiot.com/Thingworx/Things/GamaNetworkServices/Services/GetNetworkData')
    .headers({'Accept': 'application/json', 'appKey': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'})
    .end(function (response) {
      writeToCsv(response.body);
    });
}


function writeToCsv(inJSON){    

        var outCSV = inJSON.rows;

    //  console.log(outCSV)

        var csv = [];

        for(var k in outCSV) {
            var items = [[outCSV[k].PowerPlant , outCSV[k].gen1min , outCSV[k].gen1minDateTime, timeConverter(outCSV[k].gen1minDateTime)]];
             for (index = 0; index < items.length; ++index) {
                csv.push(items[index].join(', ') + '\n');
             }

             fs.writeFile(outFilename, csv, function (err) {
                if (err) {
                    return console.log(err);
                }

                console.log("Added one row!")

             });
        }

    }

function timeConverter(UNIX_timestamp){
             var a = new Date(UNIX_timestamp*1000);
             var hour = a.getUTCHours();
             var min = a.getUTCMinutes();
             var sec = a.getUTCSeconds();
             var time = hour+':'+min+':'+sec ;
             return time;
    }

If anyone can help I would be appreciated.

2 个答案:

答案 0 :(得分:2)

首先从post方法中删除 setInterval 。(你不想每分钟获得所有行)intead一次调用。 然后设置每分钟的间隔消耗一行。 该方法将消耗一行,并将为下一次迭代增加最后一个消耗行的 k 偏移量。

注意:代码未经过测试但应该完成工作。

希望它有所帮助

ConcurrentMessageListenerContainer

`

答案 1 :(得分:1)

post函数上的setInterval不是必需的,这只是继续检索数据。在每分钟写行时,您基本上都在考虑以迭代方式添加延迟。你需要为此使用setTimeout,但是重做循环后面的逻辑,这样它们就不会相互绊倒,因为setTimeout会立即返回。

function writeToCsv(rows) {
  var lineNum = 0;
  addCSVLine(); //Run the function below;
  function addCSVline() {
    var line = rows[lineNum]; //Current line
    /*
       Your code here to create the data and add this row to the file
    */
    lineNum++ //Increment our line variable
    if (lineNum < rows.length) setTimeout(addCSVline, 60000); //recursion, if we haven't reached the last row, run again;
  }
}

这是延迟的一个例子,由于显而易见的原因,我增加了一个较小的延迟。 - https://jsfiddle.net/ssc8xhjq/