简体   繁体   English

处理异步等待将 Firebase 与本机反应一起使用

[英]Handling Async Await to use Firebase with 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

This is my code and variable chatroomAlready returns '' But if I fast-refresh my app with react-native's function, It returns the expected value very well I think this problem happens because my less understanding of async-await.这是我的代码和变量 chatroomAlready 返回 '' 但是如果我用 react-native 的 function 快速刷新我的应用程序,它会很好地返回预期值我认为这个问题的发生是因为我对 async-await 的理解较少。 Any helps or clues can I solve this problem?我可以解决这个问题的任何帮助或线索吗?

这是我的火力基地

i would rather use the following syntaxe:我宁愿使用以下语法:

queryRef.where('participant', '==',userID).where('host','==',findParticipant).get().then((snapshot)=>(
DO YOUR STUFF))

Since queryRef act as a promise.由于 queryRef 充当 promise。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM