mightycode Newton
mightycode Newton

Reputation: 3949

Property 'substring' does not exist on type 'ArrayBuffer'.ts(2339)

I updated Angular 6 to Angular 8. And everything seems to work. Except for one thing.

I googled on this: Property 'substring' does not exist on type 'ArrayBuffer'.ts(2339). But didnt got any satisfactory answares

So I have this:

 const base64Img = fileReader.result.substring( fileReader.result.indexOf( ',' ) + 1 );

and the result property looks like this:

readonly result: string | ArrayBuffer | null;

But now I get this error:

Property 'substring' does not exist on type 'string | ArrayBuffer'.
  Property 'substring' does not exist on type 'ArrayBuffer'.ts(2339)

So my question is: how to fix this?

Thank you

That you not get anymore a compiler error

The whole function looks like this:

 loadImage( event: Event ) {
    const fileInput = event.target as HTMLInputElement;
    this.selectedFileName = fileInput.files[ 0 ].name;
    if ( fileInput.files[ 0 ] ) {
      const fileReader = new FileReader();
      fileReader.addEventListener( 'load', () => {
        const base64Img = fileReader.result.substring( fileReader.result.indexOf( ',' ) + 1 );
        this.form.controls.picture.setValue( base64Img );
        this.form.controls.uploadPicture.setValue( true );
      } );
      fileReader.readAsDataURL( fileInput.files[ 0 ] );
    } else {
      this.form.controls.picture.setValue( this.profile.picture );
      this.form.controls.uploadPicture.setValue( false );
    }
  }

Upvotes: 0

Views: 3500

Answers (1)

abd995
abd995

Reputation: 1839

You can do this by typecasting result to string type.

  const base64Img = (fileReader.result as string).substring((fileReader.result as string).indexOf( ',' ) + 1 );

Upvotes: 5

Related Questions