0

I'm trying to parse a csv file using Angular's ngx-papaparse. But when I'm trying to get the data from callback function, it always throws the error mentioned in the title. Here is my ts file.

export class AppComponent {
  csv: any;
  cols: any;

  constructor(private http: HttpClient, private papa: Papa) {
  }

  ngOnInit() {

  }

  parseCsvFile(file, callBack) {
    this.papa.parse(file, {
      header: true,
      dynamicTyping: true,
      skipEmptyLines: "greedy",
      worker: true,
      complete: function(results) {
        callBack(results);
      }
    });
  }

  handleUpload($event: any) {
    const fileList = $event.srcElement.files;
    this.parseCsvFile(fileList[0], this.retrieveData);
  }

  retrieveData(results) {
    console.log(results.data);
    console.log(results.meta.fields);
    this.csv = results.data;
  }
}

Error appears on this line:

this.csv = results.data;

What I'm trying to do here is to store the parsed data into this global variable and make it usable later. But after I upload my csv file, it always throws the error: Cannot set property 'csv' of undefined.

Stuart
  • 1
  • Use: `this.parseCsvFile(fileList[0], this.retrieveData.bind(this))`. Please see attached post for more info. – ruth Jul 12 '21 at 08:38

0 Answers0