Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firebase存储无提示地失败?_Javascript_Firebase_Firebase Storage - Fatal编程技术网

Javascript Firebase存储无提示地失败?

Javascript Firebase存储无提示地失败?,javascript,firebase,firebase-storage,Javascript,Firebase,Firebase Storage,我正在尝试获取多个图像的下载url,然后在我的应用程序中触发更改。但是如果这些图像中的一个因任何原因而不存在,那么一切都会悄无声息地失败 代码如下: const promises = []; snapshot.forEach(childSnapshot => { const child = childSnapshot.val(); const promise = firebase.storage() .ref(child.songImagePath) .getDo

我正在尝试获取多个图像的下载url,然后在我的应用程序中触发更改。但是如果这些图像中的一个因任何原因而不存在,那么一切都会悄无声息地失败

代码如下:

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
将图像的位置存储在存储器中。如果所有图像的所有路径都有图像,那么一切都会完美工作

但是,如果上传出错,或者由于某种原因,存储位置中没有图像,则会自动失败。我的
中没有人着火。和
承诺。所有的
永远不会解决

这是怎么回事?在调用
getDownloadURL
之前,是否有方法检查文件是否存在

编辑:正如@mjr指出的,在文档中,他们的错误回调的格式与我的略有不同。这似乎也不会引发错误,不过:

.then(
    imageURL => {
        return imageURL;
    },
    err => {
        console.log('caught', err);
        return "";
    }
);

Firebase存储JS开发人员

我在Chrome和React Native中运行了您的代码,但没有看到这种行为

我看到Promise.all总是解析(从不失败),数组中的空字符串表示无效文件。这是因为
getDownloadURL
.catch
处理程序返回空字符串

对于进一步的故障排除,了解以下信息将非常有用:

  • 您正在使用的firebase JS库的版本
  • 浏览器/环境和版本
  • 网络日志,例如来自Chrome的开发工具中的网络面板,或其他浏览器的类似日志
谷歌集团往往是一个更好的开放式故障排除的地方,有更多的来回

[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);
  });

使用不同的方式格式化错误捕获,与您在此处执行的方式不同。我会尝试这种方式,看看我注意到了什么。似乎也不会抛出错误回调。我将编辑问题以显示。使用它将比使用
forEach
中将元素推送到数组中更为惯用。是的,这会很好。但该快照不是数组,而是firebase快照,因此没有映射功能。但我同意,如果他们加一个,那就好了。