larz larz - 6 months ago 42
Node.js Question

Writes [object Object] to csv file using fast-csv

I´m using

fast-csv
to export some data from a DB to a CSV file.

When I use the code from the example in the docs :

var csvStream = csv.createWriteStream({headers:true}),
writableStream = fs.createWriteStream('./csv/list.csv');

writableStream.on('finish', function(){
console.log('DONE!');
});

csvStream.pipe(writableStream);
csvStream.write(
[
{
a: "a1",
b: "b1",
c: "c1"
}
]
);
csvStream.end();

res.send('export done!')


my csv file have one entry
[object Object]

Answer

It looks like csvStream.write() will only accept object arguments:

csvStream.write({
  a: "a1",
  b: "b1",
  c: "c1"
});

If you want to write arrays, you should use csv.write() or csv.writeToStream() (documented here, search for "Writing data" as I can't link to it directly).