0

i am using json2csv in tow routes and passing array of objects while it's working perfect on single route and on 2nd route it sums all the values in single _id column and rest colums like $init, $, error etc.

Ihtisham Tanveer
  • 338
  • 4
  • 15

1 Answers1

0

when this issue arise define your own fileds that you want to show in columns name respective to the data.


cont fileds = ['filed1', 'filed2', 'filed3'....];
const opts = {fileds};

and pass the opts as second parameter to the json2csv like

const csvString = json2csv.parse(data, opts);
res.setHeader('Content-disposition', 'attachment; filename=file-name.csv');
res.set('Content-Type', 'text/csv');
return res.status(200).send(csvString);

and Bingo everything works fine.

Ihtisham Tanveer
  • 338
  • 4
  • 15