1

I'm trying to parse a local file with PapaParse but the console returns undefined. Also, if I comment out the download:true the console returns an empty array. Is there something wrong with the way I'm passing the local file as an argument because in an async code Papa.parse('./Clean.csv',{}) works

const parseData = (content) => {
        let data;
        return new Promise((resolve) => {
            Papa.parse(content, {
                header: true,
                download: true,
                delimiter: ',',
                dynamicTyping: true,
                complete: (results) => {
                    data = results.data;
                }
            });
            resolve(data);
        });
    };

    parseData('./Clean.csv')
        .then(results => console.log(results));
Kartik Tyagi
  • 25
  • 1
  • 5

1 Answers1

0

You can pass a file stream to Papa Parse, this works nicely for server side code:

const Papa = require('papaparse');
const fs = require('fs');

const parseData = (content) => {
    const fileStream = fs.createReadStream(content);
    return new Promise((resolve) => {
        Papa.parse(fileStream, {
            header: true,
            delimiter: ',',
            dynamicTyping: true,
            complete: (results) => {
                resolve(results);
            }
        });
    });
};

parseData('./Clean.csv')
    .then(results => console.log(results));
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40