markvgti
markvgti

Reputation: 4619

Can't get NodeJS to write anything to a writable stream

Running NodeJS (v8.16.2) locally on the command line. I scrape an e-commerce website, gather the relevant information into a data-structure, then try to write it into a plain-text CSV file (my records don't have a fixed set of fields) manually by creating a write stream. This last step isn't working.

// Other stuff

const exitHandler = function(options, exitCode) {
    if (exitCode || exitCode !== 0) console.log(exitCode);
    // Other stuff
    writeToCsv();
    if (options.exit) process.exit();
}

const writeToCsv = function() {
    let ws = fs.createWriteStream('./final-data.csv');
    const crlf = '\n\r'; // might need to reverse this

    // Please ignore the weird layout
    for (let seller in finalData.sellers) {
        ws.write('Seller:,' + seller + crlf + ',Brands:');
        for (let brand of finalData.sellers[seller].brands) {
            ws.write(',' + brand);
        }
        ws.write(crlf + ',Addresses:');
        for (let addr of finalData.sellers[seller].addrs) {
            ws.write(',"' + addr + '"');
        }
        ws.write(crlf);
    }
    ws.on('finish', () => {
        console.log('Wrote all data'); // never prints this
    });
    ws.end();
}

process.on('exit', exitHandler.bind(null,{cleanup:true}));

I suspect this is because NodeJS exits before the data has been flushed to disk, but can't figure out a way to make NodeJS flush the data synchronously.

PS: new to NodeJS

Upvotes: 0

Views: 156

Answers (1)

Chirag
Chirag

Reputation: 313

please check out below example and integrate it as per your comment i updated code

async function writeDataInCSV(filePath, dynamicHeader, data) {
    const csvWriter = createCsvWriter({
      path: filePath,
      header: dynamicHeader
    });
    await csvWriter
      .writeRecords(data)
      .then(()=> console.log('The CSV file was written successfully'));
} 

writeDataInCSV('out.csv',dynamicHeader, Data)

here set array of header and make data and pass to writeDataInCSV method

Upvotes: 0

Related Questions