0

I'm trying to converte a csv local file to json and send it to an API.

But I have a problem with each function that I've found around.

I pick the csv file from event target like this

const csvAsJson = this.csvJSON(event.target.files[0])

Then in csvJSON function I have this:

csvJSON = csv => {
    console.log(csv)
    var lines=csv.split("\n");
    // more code...

But I get an error at the split "csv.split is not a function".

In the console.log(csv) I got this:

File {name: "my file name.csv", lastModified: 1578504279032, lastModifiedDate: Wed Jan 08 2020 14:24:39 GMT-0300 (hora de verano de Chile), webkitRelativePath: "", size: 8809, …}
name: "my file name.csv"
lastModified: 1578504279032
lastModifiedDate: Wed Jan 08 2020 14:24:39 GMT-0300 (hora de verano de Chile) {}
webkitRelativePath: ""
size: 8809
type: "application/vnd.ms-excel"
__proto__: File

How should I use the split in a csv that is a file? I don't get it.

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
pmiranda
  • 7,602
  • 14
  • 72
  • 155
  • 1
    `event.target.files[0]` gives you the file object, not a string containing the contents of a file. – Quentin Jan 08 '20 at 17:53
  • @MichaelRodriguez — `event.target.files[0]` implies they are working a browser, not in Node.js. The duplicate covers how to read a file there. – Quentin Jan 08 '20 at 18:04

0 Answers0