2016-09-16 53 views
1

我試圖獲取多個圖像的下載網址,然後在我的應用程序中觸發更改。但是......如果其中一張圖片因任何原因而不存在,那麼一切都會默默無聞。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 ""; 
    } 
); 
+0

[該文檔(https://firebase.google.com/docs/storage/web/download-files)有不同的方式格式化從你在這裏完成的錯誤。我會試試這種方式,看看會發生什麼 – mjr

+0

我注意到了。似乎也不會拋出錯誤回調。我將編輯顯示的問題。 – nicholas

+0

使用['Array.prototype.map'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)而不是'push從'forEach'中將元素存儲到數組中。 – royhowie

回答

1

Firebase存儲JS開發這裏。

我在Chrome和React Native中對代碼進行了微小的更改[1],但沒有看到該行爲。

我看到Promise.all始終解決(永遠不會失敗),在數組中有一個空字符串用於無效文件。這是因爲getDownloadURL.catch處理函數返回一個空字符串。

對於進一步的故障排除,這將是有益的知道:

  • 版本使用的是
  • 瀏覽器/環境和版本
  • 網絡日誌的火力JS庫,例如從網絡Chrome開發工具中的面板或其他瀏覽器的類似面板

The firebase-talk Google Group往往是開放式問題排查與更多來回。

[1]作爲參考,這是我的代碼:

const promises = []; 

// Swap out the array to test different scenarios 

// None of the files exist. 
//const arr = ['nofile1', 'nofile2', 'nofile3']; 

// All of the files exist. 
const arr = ['legitfile1', 'legitfile2', 'legitfile3']; 

// Some, but not all, of the files exist. 
//const arr = ['legitfile1', 'nofile2', 'nofile3']; 

arr.forEach(val => { 
  const promise = firebase.storage() 
    .ref(val) 
    .getDownloadURL() 
    .catch(err => { 
     // This runs for nonexistent files 
      console.log('caught', err); 
      return ""; 
    }) 
    .then(imageURL => { 
     // This runs for existing files 
      return imageURL; 
    }); 

  promises.push(promise); 
}); 

Promise.all(promises) 
  .catch(err => { 
    // This never runs 
    console.log('caught', err); 
  }) 
  .then(urls => { 
    // This always runs 
    console.log('urls', urls); 
  });