【问题标题】:How do I get user details in Firebase Storage?如何在 Firebase 存储中获取用户详细信息?
【发布时间】:2021-11-05 01:16:34
【问题描述】:

我是一名新程序员,对 firebase 非常陌生,我正在尝试让当前用户文件信息显示在屏幕上,看来我的问题是我可以分别获取 URL 和元数据,如何我把它们结合起来吗?我怎样才能一次拿走所有东西? 我需要显示文件名、日期、时间、下载链接。

const getUserFiles = async () => {
  if (!userUID) {
    return null;
  }
  let listRef = storageRef.child(userUID);
  listRef.listAll().then(res => {
    // res.prefixes.forEach((item) => {
    // });
    res.items.forEach(item => {
      item.getMetadata().then(item => {
        var file = {
          name: item.name.toString(),
          timeCreated: item.timeCreated.toString(),
          link: '',
        };
        myFiles.push(file);
      });
    });
    res.items.forEach(item => {
      let counter = 0;
      item.getDownloadURL().then(url => {
        myFiles[counter].link = url.toString();
      });
    });
  });
  console.log(myFiles);
};

当前方法不起作用!并注意 userUID 它只是没有用户的 uid(本地状态)

谢谢!

【问题讨论】:

    标签: javascript reactjs firebase-storage


    【解决方案1】:

    问题在于异步调用。您正在 forEachforEach expects a synchronous function 中进行异步调用。

    您可以更改逻辑以改用for-of

    见下文:

    const getUserFiles = async () => {
      if (!userUID) {
        return null;
      }
    
      let listRef = storageRef.child(userUID);
    
      const res = await listRef.listAll();
    
      for (const itemRef of res.items) {
        const itemMetadata = await itemRef.getMetadata();
        const url = await itemRef.getDownloadUrl();
    
        var file = {
          name: itemMetadata.name.toString(),
          timeCreated: itemMetadata.timeCreated.toString(),
          link: url,
        };
    
        myFiles.push(file);
      }
    
      console.log(myFiles);
    
    }
    

    【讨论】:

    • 感谢您的回复!当我复制它时出现此错误: 第 24:14 行:'itemRef' 未定义 no-undef 第 25:38 行:'itemRef' 未定义 no-undef 第 26:29 行:'itemRef' 是没有定义 no-undef ,我不明白为什么。它应该迭代 res.items
    • 我已经更新了代码。在for 循环中,itemRef 之前应该有const
    猜你喜欢
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 2023-02-22
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多