Mateen Kazia
Mateen Kazia

Reputation: 349

Write result into csv file in nodeJS

I'm trying to loop through a string of url images then grab the 3 rgb value colors which splashy.js does, then using csv-writer to display the data in csv file, but unfortunately instead of adding to the file csv-writer keeps rewriting the file rather then adding to the file. How to fix this?

enter image description here

Upvotes: 1

Views: 13479

Answers (3)

ryu1kn
ryu1kn

Reputation: 487

If you just add append: true to your createCsvWriter call argument, it would somewhat work, though the order of CSV records depends on the order splashy.fromUrl finishes its work. Also, because you use append mode, you won't get the header record in the output CSV.

Instead, you can define csvWriter outside of your imgs loop.

const csvWriter = createCsvWriter({
  path: "./data/output.csv",
  header: [
    { id: "url", title: "URl" },
    { id: "color1", title: "Color" },
    { id: "color2", title: "Color" },
    { id: "color3", title: "Color" }
  ]
});

const promiseForRecords = imgs.map(async img => {
  const colors = await splashy.fromUrl(img.toString());
  return {
    url: img,
    color1: colors[0],
    color2: colors[1],
    color3: colors[2]
  };
});

Promise.all(promiseForRecords)
  .then(records => csvWriter.writeRecords(records))

This way, the record order is guaranteed and you'll get the header record in the first line.

The advantage of using csv-writer library here is that you don't need to worry whether any image URLs contain comma , or double-quotes " which need to be escaped in CSV.

If you do want to write CSV line-by-line, you can use .reduce instead of .map like this:

imgs.reduce(async (promise, img) => {
  await promise;

  const colors = await splashy.fromUrl(img.toString());
  const records = [
    {
      url: img,
      color1: colors[0],
      color2: colors[1],
      color3: colors[2]
    }
  ];
  return csvWriter.writeRecords(records);
}, Promise.resolve());

Upvotes: 1

naman bhardwaj
naman bhardwaj

Reputation: 51

This can also be achieved without depending on any 3rd party npm module . We can generate csv files by use core node.js functionalities.

For that case I need to understand in what format you are looking for the csv file.

A basic csv file can be generated as :

let headers = ["h1","h2","h3","h4"].join("\t");
let row1    = ["r1","r2","r3","r4"].join("\t");
let row2    = ["t1","t2","t3","t4"].join("\t");

let writeStream = headers+"\n"+row1+"\n"+row2+"\n";

let fs = require("fs");

fs.writeFile("file.csv", writeStream)
.then(file => {{file details}})
.catch(err => err)

Upvotes: 3

aadlani
aadlani

Reputation: 661

On the first line, csv-writer#createobjectcsvwriter accepts an optional parameter to specify that you want to append to the file instead of overwriting it. You can find the details on the npm package description

createObjectCsvWriter(params) Parameters:

params <Object>
   - append <boolean> (optional)
     Default: false. When true, it will append CSV records to the specified file. If the file doesn't exist, it will create one.

Your line should read something like:

const createCsvWriter = require("csvwriter").createObjectCsvWriter({ append: true }) 

Upvotes: 3

Related Questions