Reactjs React本机异步承诺永远挂起
我正在使用Firebase Firestore,我正在使用三个等待。这背后的逻辑是,如果firestore集合中没有任何内容,则输出不应返回,因此输出应仅由一个函数设置。我想知道这三个等待是否导致它永久挂起,或者是否有一些东西我无法理解async/await。我是新手,所以任何资源或答案都有帮助。谢谢大家!Reactjs React本机异步承诺永远挂起,reactjs,react-native,asynchronous,google-cloud-firestore,async-await,Reactjs,React Native,Asynchronous,Google Cloud Firestore,Async Await,我正在使用Firebase Firestore,我正在使用三个等待。这背后的逻辑是,如果firestore集合中没有任何内容,则输出不应返回,因此输出应仅由一个函数设置。我想知道这三个等待是否导致它永久挂起,或者是否有一些东西我无法理解async/await。我是新手,所以任何资源或答案都有帮助。谢谢大家! componentDidMount() { // sends to different home screen based on authentication
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>
)
}
}
componentDidMount(){
//根据身份验证发送到不同的主屏幕
异步函数身份验证开关(cred){
var输出;
等待firebase.firestore().collection('Students')。其中('uid',“==”,cred)。get()。然后(函数(querySnapshot){
querySnapshot.forEach(函数(doc){
输出=“学生”;
});
});
等待firebase.firestore().collection('Admin')。其中('uid',“==”,cred)。get()。然后(函数(querySnapshot){
querySnapshot.forEach(函数(doc){
控制台日志(文档id);
output=“admin”;
});
});
等待firebase.firestore().collection('Preceptors')。其中(“uid”,“==”,cred)。get()。然后(函数(querySnapshot){
querySnapshot.forEach(函数(doc){
output=“preceptor”;
});
});
返回输出;
}
firebase.auth().onAuthStateChanged(用户=>{
如果(用户){
console.log(user.uid);
log(authSwitch(user.uid));
this.props.navigation.navigate(authSwitch(user.uid));
}
否则{
this.props.navigation.navigate(“Auth”);
}
});
}
render(){
返回(
加载。。。
)
}
}
您有一个不返回任何内容的then()
。据我所知,这意味着你正在接受承诺,wait
正在期待
据我所知,编写这些查询和承诺的正确方法是:
async function authSwitch(cred) {
var output;
let querySnapshot = await firebase.firestore().collection('Students').where("uid", "==", cred).get();
querySnapshot.forEach(function (doc) {
output = "student";
});
querySnapshot = await firebase.firestore().collection('Admin').where("uid", "==", cred).get()
querySnapshot.forEach(function (doc) {
console.log(doc.id);
output = "admin";
});
querySnapshot = await firebase.firestore().collection('Preceptors').where("uid", "==", cred).get();
querySnapshot.forEach(function (doc) {
output = "preceptor";
});
return output;
}
感谢您如此快速的响应,但是这会在控制台中返回相同的结果。一个承诺被返回,但是在承诺之后,另一个函数有一个console.log,它永远不会打印出来。如果问题在那里,你能给我们看一下另一个函数吗?是的,我编辑了原始帖子来显示另一个函数。你在那里缺少一个
wait
:console.log(wait authSwitch(user.uid))代码>在函数之外