Deleting Local Ionic Files 4

Asked

Viewed 199 times

0

I basically made an application where the user records an audio, and then is presented to him with 2 options, Play and Remove. However my remove function is not working and Logcat neither debug shows error.

Remove function:

deleteAudio(){
    if (this.platform.is('ios')) {
     this.file.removeFile(this.file.documentsDirectory, this.fileName)
   } else if (this.platform.is('android')) { 
    this.file.removeFile(this.file.externalDataDirectory, this.fileName) 
    }
   }

I’m doing something wrong?

Function call:

<ion-button slot="end" (click)="deleteAudio()">
        <ion-icon name="trash"></ion-icon>
      </ion-button>

Function that works the callback:

  getAudioList() {
    if(localStorage.getItem("audiolist")) {
      this.audioList = JSON.parse(localStorage.getItem("audiolist")).then( data =>{
        console.log('Funciona');
      }).catch( error => {
        console.log('Erro') ;
      });
      console.log(this.audioList);
    }
  }

1 answer

0

Oops, removeFile is an asynchronous function, in which case it returns a Promise, you have to wait for Promise’s callback or use async/await, it follows the two forms below:

Callback

deleteAudio(){
  if (this.platform.is('ios')) {
    this.file.removeFile(this.file.documentsDirectory, this.fileName).then( data => {
      alert('Sucesso');
    }).catch( error => {
      alert('Erro') ;
    });
  } else if (this.platform.is('android')) { 
    this.file.removeFile(this.file.externalDataDirectory, this.fileName).then( data => {
      alert('Sucesso');
    }).catch( error => {
      alert('Erro') ;
    });
  }
}

Async/Await

async deleteAudio(){
  if (this.platform.is('ios')) {
    await this.file.removeFile(this.file.documentsDirectory, this.fileName)
  } else if (this.platform.is('android')) { 
    await this.file.removeFile(this.file.externalDataDirectory, this.fileName)
  }
}
  • Both methods did not work and nothing appears in the logs

  • Try to put Alert in the callbacks, like I did now.

  • I did, he doesn’t call me back. I did a test putting a callback in another function that is already working and it worked, however in the function of deleting the file not.

  • Place the call and how is the other function in the question.

  • I put in the issue.

  • Try doing without if Else. Maybe the validation might not be right.

Show 1 more comment

Browser other questions tagged

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