• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: error when deleting storage data if data don’t exists when using deleteObject(storageRef)?

Resolved: error when deleting storage data if data don’t exists when using deleteObject(storageRef)?

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I am creating a delete user data function. One part of this function is to clean storage. If the user don’t have an image to begin with, an error is generated halting the exception of the rest of the function, how to prevent this error form happening, without having to check and make a get request to see if there is data in storage to begin with ?
Error message : Object 'profiles/id' does not exist. (storage/object-not-found)
// Delete user's Storage Data
const storage = getStorage();
const storageRef = ref(storage, `profiles/${currentUser.uid}`);
await deleteObject(storageRef);
// Delete the user 
await deleteUser(currentUser);

Answer:

As mentioned by @JohnHanley, you can use exception handling and catch the error.
See sample code below on how to implement exception handling:
const storage = getStorage();

const storageRef = ref(storage, `profiles/${currentUser.uid}`);

await deleteObject(storageRef).then(() => {
    console.log("File deleted successfully!")
  }).catch((error) => {
    // Do something if error occured.
    if (error.code == 'storage/object-not-found') {
        console.log('Object not found!')
    }
  });
// Delete the user 
await deleteUser(currentUser);
You can refer to this documentation for other available error messages.

If you have better answer, please add a comment about this, thank you!

firebase firebase-storage google-cloud-storage
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: In Bigquery How iterate the records as per counter in the table

27/03/2023

Resolved: Java Swing center JLabel in JPanel with two JLabels

27/03/2023

Resolved: How to compare two text files and and change the sign of the data in powershell?

27/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.