1

I just wanted to read the last line of my text file. I followed the answer on this TypeScript (Angular) - read text file line by line but the error appear and it says 'split' does not exist on type 'string | ArrayBuffer'

fileChanged(e) {
    this.file = e.target.files[0];
    if(this.file){
        let fileReader = new FileReader();
        fileReader.onload = (e) => {
            console.log(fileReader.result);
            var contents=fileReader.result;
            for (const line of contents.split(/[\r\n]+/)){
            console.log(line);
                }
        }
        fileReader.readAsText(this.file);
}
Umbro
  • 1,984
  • 12
  • 40
  • 99
mynameisx
  • 221
  • 3
  • 16

1 Answers1

1

The result you are getting is in string | ArrayBuffer type, not string type. In order to use split method cast the result into string.

const line of (contents as string).split(/[\r\n]+/)
Kassa
  • 306
  • 1
  • 8