1

I'm trying to append new data to the csv file using the following code

    const fastcsv = require('fast-csv');
    const fs = require('fs');
    const data = require('./toGenerateResponseData')
    
    
    async function writeExcel() {
        const filename = "out.csv"
        const ws = fs.createWriteStream(filename, { flags: 'a', 
        includeEndRowDelimiter: true });
    
        let reponse = await data.toGenerateResponseData();
        if (!fs.existsSync(filename)) {
            fastcsv.write(reponse, { headers: true})
                .pipe(ws);
        } else {
            fastcsv.write(reponse, { headers: false })
                .pipe(ws);
        }
        console.log(reponse);
}

Whenever I run the above code for the first time its runs as expected, but when I try to run for the second time the data is not properly appended. Its appended to the last line from the previous data.

This is how I get the data on running the code for the second time

userid,exid,level,totalScore,score,date,no_of_attempts
Ishaintermediate@gmail.com,CBO1,1,10,9,2022-09-24,1
Ishaintermediate@gmail.com,CBO2,1,10,9,2022-09-25,1
Ishaintermediate@gmail.com,CBO3,1,10,9,2022-09-26,1
Ishaintermediate@gmail.com,CBO4,1,10,9,2022-09-27,1
Ishaintermediate@gmail.com,CBO5,1,10,10,2022-09-28,2
Ishaintermediate@gmail.com,CBO13,2,10,8,2022-09-29,1
Ishaintermediate@gmail.com,CBO13,2,10,8,2022-09-30,1
Ishaintermediate@gmail.com,CBO13,2,10,9,2022-10-01,2
Ishaintermediate@gmail.com,CBO13,2,10,9,2022-10-02,2
Ishaintermediate@gmail.com,CBO13,2,10,10,2022-10-03,3
Ishaintermediate@gmail.com,CBO13,2,10,10,2022-10-04,3
Ishaintermediate@gmail.com,CBO13,2,10,9,2022-10-05,2
Ishaintermediate@gmail.com,CBO13,2,10,10,2022-10-06,3
Ishaintermediate@gmail.com,CBO13,2,10,10,2022-10-07,3
Ishaintermediate@gmail.com,CBO13,2,10,10,2022-10-08,3
Ishaintermediate@gmail.com,CLCF3,3,10,9,2022-10-09,3
Ishaintermediate@gmail.com,CLCF3,3,10,9,2022-10-10,3
Ishaintermediate@gmail.com,CLCF3,3,10,9,2022-10-11,3
Ishaintermediate@gmail.com,CLCF3,3,10,10,2022-10-12,4
Ishaintermediate@gmail.com,CLCF3,3,10,9,2022-10-13,3
Ishaintermediate@gmail.com,CLCW3,4,20,12,2022-10-14,2
Ishaintermediate@gmail.com,CLCW3,4,20,12,2022-10-15,2
Ishaintermediate@gmail.com,CLCW3,4,20,0,2022-10-16,5
Ishaintermediate@gmail.com,CLCW3,4,20,12,2022-10-17,2
Ishaintermediate@gmail.com,CLCW3,4,20,16,2022-10-18,3userid,exid,level,totalScore,score,date,no_of_attempts
Ishaintermediate@gmail.com,CBO1,1,10,10,2022-09-24,2
Ishaintermediate@gmail.com,CBO2,1,10,9,2022-09-25,1
Ishaintermediate@gmail.com,CBO3,1,10,9,2022-09-26,1
Ishaintermediate@gmail.com,CBO4,1,10,10,2022-09-27,2
Ishaintermediate@gmail.com,CBO5,1,10,9,2022-09-28,1
Ishaintermediate@gmail.com,CBO13,2,10,10,2022-09-29,3
Ishaintermediate@gmail.com,CBO13,2,10,10,2022-09-30,3
Ishaintermediate@gmail.com,CBO13,2,10,10,2022-10-01,3
Ishaintermediate@gmail.com,CBO13,2,10,9,2022-10-02,2
Ishaintermediate@gmail.com,CBO13,2,10,9,2022-10-03,2
Ishaintermediate@gmail.com,CBO13,2,10,8,2022-10-04,1
Ishaintermediate@gmail.com,CBO13,2,10,10,2022-10-05,3
Ishaintermediate@gmail.com,CBO13,2,10,8,2022-10-06,1
Ishaintermediate@gmail.com,CBO13,2,10,10,2022-10-07,3
Ishaintermediate@gmail.com,CBO13,2,10,8,2022-10-08,1
Ishaintermediate@gmail.com,CLCF3,3,10,9,2022-10-09,3
Ishaintermediate@gmail.com,CLCF3,3,10,10,2022-10-10,4
Ishaintermediate@gmail.com,CLCF3,3,10,9,2022-10-11,3
Ishaintermediate@gmail.com,CLCF3,3,10,10,2022-10-12,4
Ishaintermediate@gmail.com,CLCF3,3,10,10,2022-10-13,4
Ishaintermediate@gmail.com,CLCW3,4,20,16,2022-10-14,3
Ishaintermediate@gmail.com,CLCW3,4,20,0,2022-10-15,5
Ishaintermediate@gmail.com,CLCW3,4,20,16,2022-10-16,3
Ishaintermediate@gmail.com,CLCW3,4,20,0,2022-10-17,5
Ishaintermediate@gmail.com,CLCW3,4,20,0,2022-10-18,5

How to resolve this issue?

  • Does this answer your question? [How to append to a file in Node?](https://stackoverflow.com/questions/3459476/how-to-append-to-a-file-in-node) and more answers [here](https://stackoverflow.com/search?q=%5Bjavascript%5D++%5Bnodejs%5D+csv+append) – Yogi Sep 24 '22 at 09:46

0 Answers0