Home > OS >  error when deleting storage data if data don't exists when using deleteObject(storageRef)?
error when deleting storage data if data don't exists when using deleteObject(storageRef)?

Time:06-16

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);

CodePudding user response:

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.

  • Related