1

I'm new at Typescript and have a syntax problem. I want to load a user selected file (not using JQuery) and print the results to the console for now. I was following some guides in JS but with no success. This is what I have so far.

index.html

<input type="file" id="file-input" >

index.ts

document.getElementById("file-input").addEventListener("change", e => {
  var reader = new FileReader();
  var self = this;
  reader.onload = file => {
      var contents: any = file.target;
      self.text = contents.result; 
  };
  console.log(self.text.toString());
}, false);  

This gives the following error in the console report:

Uncaught TypeError: Cannot read property 'toString' of undefined
    at HTMLInputElement.<anonymous>

I'm not quite sure how to get the text from the buffer or if I'm even setting it up correctly.

UPDATE: Current code with modifications:

document.getElementById("file-input").addEventListener("change", async (e) => {
  var file = (<HTMLInputElement>e.target).files[0];
  var reader = new FileReader();
  reader.onload = file => {
      var contents: any = file.target;
      this.text = contents.result; 
      console.log(this.text.toString());
  };
}, false);
6
  • You need to wait for it to load. Commented May 2, 2019 at 21:49
  • 1
    Move the console.log inside the callback, which is called asynchronously, when the file is finally loaded, long after the file input change event listener function has returned. Commented May 2, 2019 at 21:52
  • 1
    Also, this self variable is unnecessary. Just use this: that's what arrow functions allow. Commented May 2, 2019 at 21:54
  • I thought that was the case @JBNizet, I moved the console.log inside the callback. However, I don't get any console display (or errors) then. I corrected the self variable usage. Commented May 2, 2019 at 21:56
  • 1
    That's because you never read the file. All you're doing it telling what to do when it's read. But you need to trigger the read: developer.mozilla.org/en-US/docs/Web/API/FileReader/onload Commented May 2, 2019 at 22:02

1 Answer 1

1

Focus in on the code

  reader.onload = file => {
      var contents: any = file.target;
      self.text = contents.result; 
  };
  console.log(self.text.toString());

The order of execution you think it has:

  reader.onload = file => {
      var contents: any = file.target;
      self.text = contents.result; // FIRST 
  };
  console.log(self.text.toString()); // SECOND 

The order of execution in reality:

  reader.onload = file => {
      var contents: any = file.target;
      self.text = contents.result; // SECOND
  };
  console.log(self.text.toString()); // FIRST 

Hence you are getting the error as .text isn't assigned by the time self.text.toString runs.

FIX

One way:

  reader.onload = file => {
      var contents: any = file.target;
      self.text = contents.result; // FIRST
      console.log(self.text.toString()); // SECOND 🌹
  };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.