gpt4 book ai didi

javascript - 处理 Async Await 以将 Firebase 与 React Native 结合使用

转载 作者:行者123 更新时间:2023-12-03 17:22:59 26 4
gpt4 key购买 nike

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
这是我的代码和变量 chatroomAlready 返回 ''
但是,如果我使用 react-native 的功能快速刷新我的应用程序,它会很好地返回预期值
我认为这个问题的发生是因为我对 async-await 的了解较少。
有什么帮助或线索可以解决这个问题吗?
this is my firebase

最佳答案

我宁愿使用以下语法:

queryRef.where('participant', '==',userID).where('host','==',findParticipant).get().then((snapshot)=>(
DO YOUR STUFF))
由于 queryRef 充当 promise 。

关于javascript - 处理 Async Await 以将 Firebase 与 React Native 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65413528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com