有没有办法在触发 getDownloadURL 选项之前检查存储是否存在?
这是我的代码。
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);
});
如果我没有配置文件图片的存储文件夹,我的控制台日志将继续显示错误,指出我没有存储对象。因此我希望它仅在存储对象存在时才运行该函数,这可能吗?
最佳答案
没有。
您必须下载来检查它是否存在。 Firebase API以正确的错误进行响应,告诉您失败的原因。
使用catch
block 来处理错误:
.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;
}
});
错误代码的完整列表已提供 here .
关于javascript - Firebase Javascript 在运行函数之前检查存储是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46127757/