我試圖獲取多個圖像的下載網址,然後在我的應用程序中觸發更改。但是......如果其中一張圖片因任何原因而不存在,那麼一切都會默默無聞。Firebase存儲失敗默默無聞?
下面的代碼:
const promises = [];
snapshot.forEach(childSnapshot => {
const child = childSnapshot.val();
const promise = firebase.storage()
.ref(child.songImagePath)
.getDownloadURL()
.catch(err => {
console.log('caught', err);
return "";
})
.then(imageURL => {
return imageURL;
});
promises.push(promise);
});
Promise.all(promises)
.catch(err => {
console.log('caught', err);
})
.then(urls => {
...do something with urls array
});
我在我的數據庫存儲在存儲圖像的位置使用child.songImagePath
。如果所有圖像的所有路徑都有圖像,則一切正常。
但是,如果上傳失敗或者由於某種原因存儲位置中沒有映像,它將自動失敗。我的catch
沒有火。而Promise.all
永遠不會解決。
這是怎麼回事?在調用getDownloadURL
之前有沒有辦法檢查文件的存在?
編輯:正如@mjr指出的那樣,他們在文檔中對它們的錯誤回調進行了格式化,與我的略有不同。這似乎從來沒有發生錯誤,但:
.then(
imageURL => {
return imageURL;
},
err => {
console.log('caught', err);
return "";
}
);
[該文檔(https://firebase.google.com/docs/storage/web/download-files)有不同的方式格式化從你在這裏完成的錯誤。我會試試這種方式,看看會發生什麼 – mjr
我注意到了。似乎也不會拋出錯誤回調。我將編輯顯示的問題。 – nicholas
使用['Array.prototype.map'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)而不是'push從'forEach'中將元素存儲到數組中。 – royhowie