Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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 从Firestore接收图像时如何等待承诺?_Javascript_Firebase_React Native_Async Await_Google Cloud Firestore - Fatal编程技术网

Javascript 从Firestore接收图像时如何等待承诺?

Javascript 从Firestore接收图像时如何等待承诺?,javascript,firebase,react-native,async-await,google-cloud-firestore,Javascript,Firebase,React Native,Async Await,Google Cloud Firestore,我正在使用React Native制作一个应用程序,并在Firestore上的一个名为“用户”的集合中存储有关用户的信息。每个用户都有一个配置文件图片url存储在集合中,我希望在同一页面上显示多个用户图像。然而,由于不得不等待承诺的回报,我正在努力让它发挥作用 我曾尝试在检索url时将其存储在状态变量中,但是,由于要显示的图像数量,这将涉及创建状态变量的负载。然后我尝试使用async/await和then语句,但是由于承诺没有及时返回,图像无法加载 async getImg(user_id) {

我正在使用React Native制作一个应用程序,并在Firestore上的一个名为“用户”的集合中存储有关用户的信息。每个用户都有一个配置文件图片url存储在集合中,我希望在同一页面上显示多个用户图像。然而,由于不得不等待承诺的回报,我正在努力让它发挥作用

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

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

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

getImg函数返回Promise。相反,如果存在img,则可以将img保存到状态并进行渲染

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>
  )
}
async getImg(用户id){
return wait wait firebase.firestore().collection('User').doc(User_id).get()
。然后(用户=>{
这是我的国家({
img:user.data().image
})
}
)
}
render(){
const{img}=this.state;
返回(
img&
)
}

getImg函数返回承诺。相反,如果存在img,则可以将img保存到状态并进行渲染

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>
  )
}
async getImg(用户id){
return wait wait firebase.firestore().collection('User').doc(User_id).get()
。然后(用户=>{
这是我的国家({
img:user.data().image
})
}
)
}
render(){
const{img}=this.state;
返回(
img&
)
}
您正在将的用法与方法混合使用

采取以下行动:

async getImg(user_id) {
    const userSnapshot = await firebase.firestore().collection('User').doc(user_id).get()
    return userSnapshot.data().image;
}
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';
    })
  }

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

我不知道react native,所以我不知道在中使用它是否有效

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

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

async getImg(user_id) {
    const userSnapshot = await firebase.firestore().collection('User').doc(user_id).get()
    return userSnapshot.data().image;
}
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';
    })
  }

}
你是在使用这个方法

采取以下行动:

async getImg(user_id) {
    const userSnapshot = await firebase.firestore().collection('User').doc(user_id).get()
    return userSnapshot.data().image;
}
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';
    })
  }

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

我不知道react native,所以我不知道在中使用它是否有效

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

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

async getImg(user_id) {
    const userSnapshot = await firebase.firestore().collection('User').doc(user_id).get()
    return userSnapshot.data().image;
}
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';
    })
  }

}

非常感谢。在这个解决方案中,我应该在哪里调用getImg函数?特别是当我想显示多个图像时?你可以调用componentDidMount()@joshuazeltsertanks。在这个解决方案中,我应该在哪里调用getImg函数?尤其是当我想显示多个图像时?您可以调用componentDidMount()@JoshuaZeltser