Search code examples
javascriptjsonnode.jscsvwritefile

Using Fs/Node/Json2Csv To Add Rows to CSV


I am streaming data off of Twitter's API, and am processing it on a node server I have set up. I am currently parsing the data so I have an object that looks like this: {"name": //name, "tweet": //textofthetweet, "url": //tweetURL}

I would like to be able to add each of these objects as new line to a CSV file, adding a new line for every tweet I receive.

Right now, I know how to create a single CSV every time I create this object. How do I tweak this code so that the new entry is added to the CSV file, rather than overwriting it?

Here's the relevant bit of code:

var param =  {follow: 'a bunch of twitter ID's go here'
var followIds = [//The same Twitter IDs go here //]
twitterClient.stream('statuses/filter',param,function(stream) {
stream.on('data', function(tweet) {

  // Variables used to store CSV update (and the CSV column names).
  var update = [];
  var columns = ["name","tweet","URL"];

  var i;
  for(i = 0; i <followIds.length; i++){
    if(followIds[i] == tweet.user.id_str){ // if so, get contents

      var tweetURL = `https://twitter.com/${tweet.user.screen_name}/status/${tweet.id_str}`

      let name = tweet.user.name;
      let tweetText = tweet.text;

      /////  PREPARE UPDATE TO CSV HERE //////
      update.push({"name": name, "tweet": tweetText, "url": tweetURL})



      ///// THIS PART OF THE CODE NEEDS TO CHANGE /////
      var csv = json2csv({ data: update, fields: columns})
        fs.writeFile('twitterData.csv',csv,function(err){
        if (err) throw err;
        console.log('File Saved')
      })
    }
     update = []; // Clear update here.
  }   
})

Does anyone know how to do this, preferably by making a small adjustment to my code? Thank you very much!


Solution

  • Use appendFile instead of writeFile.