Search code examples
typescriptfirebaseasynchronousfirebase-storageangularfire

Getting "delete(...).then(...).error is not a function" when deleting a file on firebase storage. AngularFire/TypeScript


I'm using the snippets Delete files with Cloud Storage on Web to delete a file from my Firebase Storage, followed by uploading a new image onto the same path. But I keep getting "delete(...).then(...).error is not a function".

Here is my code:

const filePath = `instructors/${instrcutorID}`;
// File referance to the resized image
const newFileRef = this.afStorage.ref(`${filePath}/cover_img_1040x585`);
// Create a reference to the file to delete 
          const desertRef = this.afStorage
            .ref(filePath)
            .child('cover_img_1040x585');
          // Delete the file
          desertRef
            .delete()
            .then(() => {
              // Upload new file
              this.afStorage.upload(`${filePath}/cover_img`, this.coverFile);
              return this.keepTrying(10, newFileRef).then((url) => {
               // When new URL ready update it in firestore.
                const coverURL = url;
                this.afs.doc<Instructor>(`instructors/${instrcutorID}`).update({
                  coverURL: coverURL,
                });
                // set loading off via RXJS
                this.loadingSource.next(false);
                alert('Successfully, updated...');
              });
            })
            .catch((err: string) => {
              // set loading off via RXJS
              this.loadingSource.next(false);
              alert('Uh-oh, an error occurred!');
              return console.error(err);
            });

Solution

  • Somehow managed to solve it by adding a promise got the info from RXJSto the delete() before .then() fixed it. Maybe this just a workaround that I'm not too sure about.

    Open to hearing some insight on this. :)

     // Create a reference to the file to delete
      const desertRef = this.afStorage
        .ref(filePath)
        .child('cover_img_1040x585');
      // Delete the file
      desertRef
        .delete()
        .toPromise() // ADDED HERE
        .then((val) => {
          this.afStorage.upload(`${filePath}/cover_img`, this.coverFile);
          this.keepTrying(10, newFileRef).then((url) => {...