Angular 7 / Typescript error: Property 'result' does not exist on type 'Element'

Asked

Viewed 581 times

1

I have a function that runs while performing the upload of files, however I am having an error in result within the function onload.

This mistake makes no sense because if I give one console.log in the result the result appears clearly, so it seems to me a false positive before the angular compiler, who accuses the error and does not compile the program.

In order to run I am commenting on that line and removing the comment, why for some reason after the application running it accuses the error but not seriously enough to stop it.

async uploadFile(event) {
    var reader = new FileReader();
    let file = event.target.files[0];

    reader.onload = ((file: any) => {
      return (e: Event) => {
        let result = e.srcElement.result;
        //aqui acontece o erro e impede de compilar mas não existe erro de fato.
      }
    })(file);

    reader.readAsText(file);
  }

2 answers

1

With Typescript I have not yet programmed, but javascript I use something as shown in the code below, I think it can help you. = D

var reader = new FileReader(),
    file = event.target.files[0];

reader._file = file;    /// Guardo a referencia do file na propria FileReader
reader.onload = function(e){
    var self = this,          /// FileReader
        file = self._file,    /// referencia que guardei
        data = self.result;   /// Dados do resultado do reader

}

reader.readAsText(reader._file);
  • 1

    I’ll take your example, thank you.

1


The solution was very simple, just remove the type Event of the variable e and add any.

async uploadFile(event) {
    var reader = new FileReader();
    let file = event.target.files[0];

    reader.onload = ((file: any) => {
      return (e: any) => {
        let result = e.srcElement.result;
        //aqui acontece o erro e impede de compilar mas não existe erro de fato.
      }
    })(file);

    reader.readAsText(file);
  }

Solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.