2016-09-16 55 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); 
  });