【问题标题】:How to wait for a promise when receiving an image from Firestore?从 Firestore 接收图像时如何等待承诺?
【发布时间】:2020-02-08 20:46:22
【问题描述】:

我正在使用 React Native 制作应用程序,并将有关用户的信息存储在 Firestore 上名为“User”的集合中。每个用户都有一个存储在集合中的个人资料图片 url,我想在同一页面上显示多个用户图像。但是,由于必须等待 Promise 返回,我正在努力让它工作。

我尝试在检索 url 时将其存储在状态变量中,但是,由于我要显示的图像数量,这将涉及创建大量状态变量。然后我尝试使用 async/await 和 then 语句,但由于承诺没有及时返回,图像不会加载。

async getImg(user_id) {
    return await firebase.firestore().collection('User').doc(user_id).get()
        .then(user => {return user.data().image})
render() {

      <SafeAreaView style={styles.container}> 
          <Image source={{uri: this.getImg('rwWa39Y6xtS1nZguswugODWndqx2') }} style={{ ... }} />
          <Image source={{uri: this.getImg('HQkCoChUe5fkZrHypYdRnrw66Rp2') }} style={{ ... }} />

      </SafeAreaView>
    );

  }

上面的代码是我最近的尝试,由于返回的是promise而不是字符串url,它返回了以下错误。

You attempted to set the key `_65` with the value `1` on an object that is meant to be immutable and has been frozen.

有人知道如何解决这个问题吗?

【问题讨论】:

    标签: javascript firebase react-native async-await google-cloud-firestore


    【解决方案1】:

    您正在混合使用 async/awaitthen() 方法。

    按照以下方式进行:

    async getImg(user_id) {
        const userSnapshot = await firebase.firestore().collection('User').doc(user_id).get()
        return userSnapshot.data().image;
    }
    

    您将声明一个异步 getImg() 函数。

    我不知道 react-native 所以我不知道它是否可以通过使用它来工作

    <Image source={{uri: this.getImg('rwWa39Y6xtS1nZguswugODWndqx2') }} style={{ ... }} />
    

    但@VolkanSahin45 解决方案,改编如下,应该可以工作:

    async getImg(user_id) {
        const userSnapshot = await firebase.firestore().collection('User').doc(user_id).get()
        this.setState({
          img: userSnapshot.data().image;
        })
    }
    

    请注意,最好使用try/catch 处理错误,如下所示:

    async getImg(user_id) {
    
      try {
        const userSnapshot = await firebase.firestore().collection('User').doc(user_id).get()
        this.setState({
          img: userSnapshot.data().image;
        })
      } catch (error) {
        this.setState({
          img: 'default_user_img.png';
        })
      }
    
    }
    

    【讨论】:

      【解决方案2】:

      getImg 函数返回 Promise。相反,如果有 img,您可以将 img 保存到 state 并渲染。

      async getImg(user_id) {
        return await firebase.firestore().collection('User').doc(user_id).get()
          .then(user => {
              this.setState({
                img: user.data().image 
              })
            }
          )
      }
      
      render() {
        const { img } = this.state;
        return(
          <SafeAreaView style={styles.container}> 
            img && <Image source={{ img }} style={{ ... }} />
          </SafeAreaView>
        )
      }
      

      【讨论】:

      • 非常感谢。在这个解决方案中,我实际上会在哪里调用 getImg 函数?特别是当我想显示多张图片时?
      • 你可以调用componentDidMount() @JoshuaZeltser
      猜你喜欢
      • 1970-01-01
      • 2020-07-19
      • 2013-09-16
      • 2019-04-29
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      • 2016-11-12
      • 2019-12-10
      相关资源
      最近更新 更多