小编KRC*_*DER的帖子

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

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 返回 '' …

javascript async-await firebase react-native google-cloud-firestore

5
推荐指数
1
解决办法
257
查看次数