0
// js function 
const generater = (csvFilePath) => {
  console.log(csvFilePath);
  Papa.parse(csvFilePath, {
    delimiter: ',',
    dynamicTyping: true,
    encoding: 'UTF-8',
    complete: function(results, file) {
      console.log(results);
      console.log(file);
    }
  });
};

// csv file content "demo.csv"

Column 1,Column 2,Column 3,Column 4
1-1,1-2,1-3,1-4
2-1,2-2,2-3,2-4
3-1,3-2,3-3,3-4
4,5,6,7

// the function is logging an empty array, can any one help with that?

Abhishek Parmar
  • 145
  • 1
  • 11

1 Answers1

1

I just figured out a way to do it. In nodejs papa-parse doesn't works the same way. So need the use fs and extract the local file content and then parse the string is more suitable.

// read the file and save content as a string
let content = fs.readFileSync(csvFilePath, { encoding: 'utf-8' });

// parse the string to object
let contentObject = parseData(content);
console.log(contentObject);

// function to parse the data synchronously of csv file
const parseData = (content) => {
  let data;
  return new Promise( (resolve) => {
    Papa.parse(content, {
      header: true,
      delimiter: ',',
      dynamicTyping: true,
      complete: (results) => {
        data = results.data;
      }
    });
    resolve(data);
  });
};
Abhishek Parmar
  • 145
  • 1
  • 11