Property 'validateFile' does not exist on type 'FileReader'

Viewed 57

I have a csv file which I need to validate. here is the function:

 let reader = new FileReader();
  reader.onload = function (e) {
      let resultFile = e.target.result
      let value = this.validateFile((<String>resultFile).split('\n'))
      if (value != "valid") {
        errors += value;
        return errors;
      }
  }

I need to do some validation inside the function validateFile.

validateFile(data) {
     if ((fileLength <= batchProp.MAX_SUBREQUESTS_WITH_OFFERS && this.includeOffers) || (fileLength <= batchProp.MAX_SUBREQUESTS_WITHOUT_OFFERS && !this.includeOffers)) {
  if (!this.checkUploadedFile(data, fileLength)) {
    this.lastRowWasValid = true;
    }
  }
}

But I keep getting this error:

Property 'validateFile' does not exist on type 'FileReader'.

Please suggest how to fix this.

1 Answers

You need to use a arrow function to access the global scope which has the validateFile function

let reader = new FileReader();
  reader.onload = function (e) => {
      let resultFile = e.target.result
      let value = this.validateFile((<String>resultFile).split('\n'))
      if (value != "valid") {
        errors += value;
        return errors;
      }
  }
Related