I want to delete a file inside storage using URL to the image file, but I am unable to do it.
I am getting this error in the console
NativeFirebaseError: [storage/object-not-found] No object exists at the desired reference.
I am also getting this error on the screen after catching the error in the console.
Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference

I tried Click To Expand
package.json: "@react-native-firebase/app": "^7.2.1",
"@react-native-firebase/auth": "^8.0.6",
"@react-native-firebase/firestore": "^7.1.7",
"@react-native-firebase/functions": "^7.1.4",
"@react-native-firebase/storage": "^7.1.4",
yarn upgrade --latest but getting the same error.
Code of the file try {
// console.log('Image url we need to delete ', this.state.photo);
const deletePostImageFromStorageRef = Storage().refFromURL(
this.state.photo,
);
await deletePostImageFromStorageRef.delete();
}
catch(err){
console.log(err);
}
I solved the above error which was on my mobile screen
Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference
I forgot to add time duration for ToastAndroid, and by making it like ToastAndroid.show("It worked", ToastAndroid.LONG); the error is gone. But I am still facing the same error with refFromURL.
I suppose its possible that your URL isn't correct. Is it a gs:// URL, or a download URL from the resource's metadata?
It is a download URL like 'https://firebasestorage.googleapis.com/v0/b/newProject-ca4cf.appspot.com/o/images%2F23568961_APHCURSyEZNtbE66BERuRutOmQh2?alt=media&token=bb03b1fa-d6b3-4822-90f8-75e23a5de615'
Note:- This is not the exact url I modified it.But the original URL is working, I can visit the image using the download URL.
I suppose its possible that your URL isn't correct. Is it a
gs://URL, or a download URL from the resource's metadata?
Yeah, I got it. I managed to get the ref of the storage by replacing '%2F' with '' .Here is the code
function getRefToStorage(URL) {
const baseURL = 'https://firebasestorage.googleapis.com/v0/b/newProject-ca4cf.appspot.com/o/';
let imagePath = URL.replace(baseURL,'');
const indexOfEndPath = imagePath.indexOF('?');
imagePath= imagePath.substring(0, indexOfEndPath);
imagePath = imagePath.replace('%2F', '/' );
return imagePath;
}
careful in that '%2F' is not randomly chosen, it is URL encoding, and if you see one character URL-encoded it is possible there will be more in the future. You should use a real url decoder to do the translation of the string for you - it appears to be built in to Javascript https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI
Thanks @mikehardy , I got your point and found the best way to decode the URL,
Here is the code
const baseURL = 'https://firebasestorage.googleapis.com/v0/b/newProject-ca4cf.appspot.com/o/';
let imagePath = URL.replace(baseURL,'');
const indexOfEndPath = imagePath.indexOF('?');
imagePath= imagePath.substring(0, indexOfEndPath);
//imagePath = imagePath.replace('%2F', '/' );
imagePath = decodeURIComponent(imagePath); //this worked
return imagePath;
}
I have tried decodeURI(imagePath) but I was getting %2F with the result
images%2F23568961_APHCURSyEZNtbE66BERuRutOmQh2
But when I used decodeURIComponent, it worked like a charm.
images/23568961_APHCURSyEZNtbE66BERuRutOmQh2