【问题标题】:React Native Async Promise Pending ForeverReact Native Async Promise 永远挂起
【发布时间】: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


    【解决方案1】:

    您有一个不返回任何内容的then()。据我所知,这意味着您正在消费 await 所期待的承诺。

    据我所知,编写这些查询和承诺的正确方法是:

    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;
    }
    

    【讨论】:

    • 感谢您如此迅速地回复,但是在控制台中返回相同的结果。返回一个 promise,但在 promise 之后另一个函数有一个 console.log,它永远不会结束打印。
    • 您能否向我们展示其他功能,以防出现问题?
    • 是的,我编辑了原始帖子以显示其他功能。
    • 您在其中缺少awaitconsole.log(await authSwitch(user.uid));
    • 那在函数之外
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 2019-11-24
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多