is there a way to check if the storage first exists before firing the getDownloadURL option?
Here is my code.
var storagedpurl = firebase.storage().ref(firebaseUser.uid + '/profilePicture/' + "displaypicture"); //URL to get current user dp
storagedpurl.getDownloadURL().then(function(url) { //Download dp from storage and display in circle img tag
var firebaseimageurl = url;
document.querySelector('img').src = firebaseimageurl;
}).catch(function(error) {
console.log(error.code);
});
In the case where I do not have the storage folder for profilepicture, my console log will keep showing the error that says I do not have the storage object. Therefore I'd like it to only run the function if the storage object exists, is it possible?
No.
You have to download to check , if it exists or not. Firebase API responds with proper error telling you why it failed.
Use the catch
block to handle errors:
.catch(function(error) {
switch (error.code) {
case 'storage/object_not_found': // <<< here you decide what to do when the file doesn't exist
// File doesn't exist
break;
case 'storage/unauthorized':
// User doesn't have permission to access the object
break;
case 'storage/canceled':
// User canceled the upload
break;
...
case 'storage/unknown':
// Unknown error occurred, inspect the server response
break;
}
});
A full list of error codes is available here .
To my best knowledge, the firebase storage designed in a way that the users request a file that only exists. Therefore non-existing file have to handled as error
See this documentation for how to handle this errors in storage
Otherwise, I would suggest you can use getDownloadUrl . which in turn returns a promise like below,
storageRef.child("file.png").getDownloadURL().then(onResolve, onReject);
function onResolve(foundURL) {
//stuff
}
function onReject(error) {
console.log(error.code);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.