ryu1kn / csv-writer

Convert objects/arrays into a CSV string or write them into a CSV file
https://www.npmjs.com/package/csv-writer
MIT License
246 stars 39 forks source link

csv-writer not overwriting csv when append is false #26

Closed johnothetree closed 5 years ago

johnothetree commented 5 years ago

Working on a personal project for maintaining an album-of-the-year list, and using csv-writer in NodeJS to maintain a csv that holds all the values (repo here). The first time I do a POST call, it overwrites the CSV correctly, but if I call it more than once, it instead appends the consecutive post calls to the existing CSV instead of overwriting as intended.

ryu1kn commented 5 years ago

Hi @johnothetree , thanks for the link to the repo, that's really helpful to understand what's happening.

csv-writer is overwriting only for the first POST request, because it's initialised only once at the beginning of the app. If you want it to overwrite every time you make the POST request, you can move the createCsvWriter call inside of .post callback function.

Also, append: false is a default behaviour, so you can safely omit it 😉

johnothetree commented 5 years ago

@ryu1kn thanks for the help, didn't think about the initialization causing what I was seeing, but that makes sense. Thanks for the help!