Reactjs Onpress等待函数完成,然后在React Native中继续
我的代码在同步方面有问题,我的程序在第二次按下按钮时会做出反应,就像第一次按下按钮时一样。 (底部有详细说明) 我有以下Firebase API函数:Reactjs Onpress等待函数完成,然后在React Native中继续,reactjs,firebase,react-native,google-cloud-firestore,Reactjs,Firebase,React Native,Google Cloud Firestore,我的代码在同步方面有问题,我的程序在第二次按下按钮时会做出反应,就像第一次按下按钮时一样。 (底部有详细说明) 我有以下Firebase API函数: export const noOpponent = (Key, setOpponent) => { console.log("key: " + Key) duettsRef .where("key", "==", Key) .where("play
export const noOpponent = (Key, setOpponent) => {
console.log("key: " + Key)
duettsRef
.where("key", "==", Key)
.where("player1", "!=", firebase.auth().currentUser.uid)
.where("player2", "==", "")
.limit(1)
.get()
.then((querySnapshot) => {
setOpponent(!querySnapshot.empty)
console.log("QuerysnapshotResult: " + !querySnapshot.empty)
})
};
现在我得到了另一个.js文档,其中包含以下代码(简称):
按下第二个按钮,我得到:
OPPONENT:true
QuerysnapshotResult: true
第二次按下按钮的结果,是我预期的第一次按下按钮的结果。。。
我想,在调用noOpponent(code,setOpponent)之后
它只是在下一行立即跳转(对手?(…
),对手仍然为空,因为它没有等待我的对手
从noOpponent函数
中的结果/更改。在第二个按钮上按下它,然后设置为真,因为第一个按钮按下了。
并在控制台日志(“QuerysnapshotResult:+!querySnapshot.empty”)之前打印出console.log(“对手:+对手)
我的程序正在等待
noOpponent
函数的结果,该函数正确设置对手
,然后跳到这一行对手?
(你需要使用异步方法或承诺,试试看。)
export const noOpponent = (Key, setOpponent) => {
console.log("key: " + Key)
duettsRef
.where("key", "==", Key)
.where("player1", "!=", firebase.auth().currentUser.uid)
.where("player2", "==", "")
.limit(1)
.get()
.then((querySnapshot) => {
setOpponent(!querySnapshot.empty)
console.log("QuerysnapshotResult: " + !querySnapshot.empty)
})
})
然后按下按钮
onPress={async () => {
await noOpponent(code, setOpponent);
console.log("OPPONENT:" + opponent),
opponent ? (
opponent == true ? (
setKeyToDuett(code),
Platform.OS === "android"?(
ToastAndroid.show("Game joined!", ToastAndroid.SHORT)):
(Alert.alert("Game joined!"))
):(
Platform.OS === "android"?(
ToastAndroid.show("Game does not exist or someone else has already joined!", ToastAndroid.SHORT)):
(Alert.alert("Game does not exist or someone else has already joined"))
)
):(
console.log("opponent is probably null")
)}
}
使用promise来实现这一点。@DipanSharma我已经试过了,但我仍然得到了反对者:…在QuerysnapshotResult之前打印出来:…所以它仍然不起作用。我也必须在“const noOpponent”上更改一些内容吗?当我像你所说的那样尝试时,在第一个按钮上按下控制台日志:对手:null,对手可能为null,QuerysnapshotResult:false,因此它仍然不工作,并且打印错误order@Grrrrrag我确实认为您必须重做
const noOpponent
,因为我可以看到您已经建立了。其中(“player2”,“==”,“)
,我认为应该始终提供作为对手?文档似乎建议使用“存在”而不是“空”,在确定快照的大小/空度之前,可能需要读取/查看快照的文档。另外,我可以问一下“对手”在哪里吗正在设置?是的@fabc,也许你的问题也是你的查询。@grrrrag你能告诉我们你是否能够解决这个问题吗?如果不能,我们将进一步研究。
export const noOpponent = (Key, setOpponent) => {
console.log("key: " + Key)
duettsRef
.where("key", "==", Key)
.where("player1", "!=", firebase.auth().currentUser.uid)
.where("player2", "==", "")
.limit(1)
.get()
.then((querySnapshot) => {
setOpponent(!querySnapshot.empty)
console.log("QuerysnapshotResult: " + !querySnapshot.empty)
})
onPress={async () => {
await noOpponent(code, setOpponent);
console.log("OPPONENT:" + opponent),
opponent ? (
opponent == true ? (
setKeyToDuett(code),
Platform.OS === "android"?(
ToastAndroid.show("Game joined!", ToastAndroid.SHORT)):
(Alert.alert("Game joined!"))
):(
Platform.OS === "android"?(
ToastAndroid.show("Game does not exist or someone else has already joined!", ToastAndroid.SHORT)):
(Alert.alert("Game does not exist or someone else has already joined"))
)
):(
console.log("opponent is probably null")
)}
}