处理异步等待以将 Firebase 与 React Native 结合使用

KRC*_*DER 5 javascript async-await firebase react-native google-cloud-firestore

const userID = firebase.auth().currentUser.uid

const checkRoomExists = async ()=>{
  var queryRef = firestore.collection("groups");
  try{
    console.log("userId: "+userID)
    var snapshot = await queryRef.where('participant', '==',userID).where('host','==',findParticipant).get()
    if (snapshot.empty) {
      console.log('No matching documents1.');
      return;
    }
    else{
      snapshot.forEach(doc => {
      setChatroomAlready(doc.id)
    })}
  }
  catch(e){
    console.log('Error getting documents', e);
  }
  finally{
    console.log("chatroomAlready :"+chatroomAlready)
  }
}

async function performCreateGroup () {
  console.log("1 :"+findParticipant)
  checkRoomExists();
  console.log("2 :"+chatroomAlready)
}
//first call 
1: ZUxSZP09fzRzndr7vhK8wr56j3J3
2: (blank)
//second call returns what I expected exactly
1: ZUxSZP09fzRzndr7vhK8wr56j3J3
2: zQN4hbgqjKhHHHc70hPn
Run Code Online (Sandbox Code Playgroud)

这是我的代码,变量 chatroomAlready 返回 '' 但是如果我使用 react-native 的函数快速刷新我的应用程序,它会很好地返回预期值,我认为这个问题发生是因为我对 async-await 的了解较少。任何帮助或线索可以解决这个问题吗?

这是我的火力基地

小智 0

我宁愿使用以下语法:

queryRef.where('participant', '==',userID).where('host','==',findParticipant).get().then((snapshot)=>(
DO YOUR STUFF))
Run Code Online (Sandbox Code Playgroud)

由于 queryRef 充当承诺。