Search code examples
angulartypescriptpapaparse

Angular 4 function loses context using papaparse


I have the following code

httpcsv2Array(event) {
    var gethttpcsv = 
    Papa.parse('https://docs.google.com/spreadsheets/d/e/yadyada/pub?output=csv', {
                        download: true,
                        header: true,
                        dynamicTyping: true//,
                        complete: this.importhttpcsv
            });
  }

importhttpcsv(results) {
    this.bformservice.deleteAll();

    results.data.forEach(item => {
      let tarr = item as bform;
      this.bformservice.bulkcreatebform(tarr);
    });
}

it appears that importhttpcsv gets launched within the context of papa.parse beacuse I get the error: TypeError: Cannot read property 'deleteAll' of undefined

I am able to use this.bformservice.deleteAll() in other functions in this same class.

I need to access functions in a different service in order to update the database. Any way around this?

Thank you


Solution

  • There is need of using Arrow function to pretend correct this

    complete: () => { 
      this.importhttpcsv();
    }