无法通过react native从Firebase存储中检索URL和所有文件

polkgigr  于 2023-03-19  发布在  React
关注(0)|答案(1)|浏览(116)

下面的代码:

import { uploadString, getDownloadURL, listAll } from 'firebase/storage';
 const listRef = sRef(storage, paramKey)
  // Find all the prefixes and items.
    listAll(listRef)
    .then((res) => {
      res.prefixes.forEach((folderRef) => {
        // All the prefixes under listRef.
        // You may call listAll() recursively on them.
      });
      res.items.forEach((itemRef) => {
        // All the items under listRef.
        console.log("ItemRef: " + itemRef)
        itemRef.getDownloadURL().then((url =>{
          console.log("Download Url: " + url)
        }))
      });
    }).catch((error) => {
      // Uh-oh, an error occurred!
      console.log(error)
    });
  return (

这是检索特定文件夹中的所有文件。同时获取每个文件的下载网址。
我得到的只有以下信息:

在firebase存储控制台中,我有4个东西:

1.为什么我只得到一件东西而不是4件东西的ItemRef?它不应该是所有4件而不是1件吗?
1.为什么url没有出现?我已经导入了getDownloadURL,但是当我仔细查看我的代码时,getDownloadURL没有突出显示,这意味着未使用。所以我从导入中复制并粘贴getDownloadURL到我的函数中,但是它仍然没有突出显示。为什么?
导入告诉我它已声明,但未读取值:

9ceoxa92

9ceoxa921#

如果你看一下import语句,就会发现getDownloadURL是一个全局函数,而不是一个引用文件的方法,所以你需要像这样使用它:

getDownloadURL(itemRef).then((url =>{
  console.log("Download Url: " + url)
}))

这与文档中关于通过URL下载文件的代码示例非常相似,因此我建议在编码时将v9文档放在手边。

相关问题