【发布时间】:2020-03-10 20:44:09
【问题描述】:
我正在使用 Firebase Firestore,我正在使用三个等待。这背后的逻辑是,如果 firestore 集合中没有任何内容,则输出不应返回,因此输出应仅由一个函数设置。我想知道这三个等待是否导致它永远挂起,或者我是否对异步/等待有什么不明白的地方。我是新手,所以任何资源或答案都有帮助。谢谢!
componentDidMount() {
// sends to different home screen based on authentication
async function authSwitch(cred) {
var output;
await firebase.firestore().collection('Students').where("uid", "==", cred).get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
output = "student";
});
});
await firebase.firestore().collection('Admin').where("uid", "==", cred).get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
console.log(doc.id);
output = "admin";
});
});
await firebase.firestore().collection('Preceptors').where("uid", "==", cred).get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
output = "preceptor";
});
});
return output;
}
firebase.auth().onAuthStateChanged(user => {
if(user) {
console.log(user.uid);
console.log(authSwitch(user.uid));
this.props.navigation.navigate(authSwitch(user.uid));
}
else {
this.props.navigation.navigate("Auth");
}
});
}
render() {
return (
<View style={styles.container}>
<Text>Loading...</Text>
<ActivityIndicator size = "large"></ActivityIndicator>
</View>
)
}
}
【问题讨论】:
标签: reactjs react-native asynchronous google-cloud-firestore async-await