Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何才能等到firebase.auth完成工作后再跳转到下一页_Reactjs_Firebase Authentication - Fatal编程技术网

Reactjs 如何才能等到firebase.auth完成工作后再跳转到下一页

Reactjs 如何才能等到firebase.auth完成工作后再跳转到下一页,reactjs,firebase-authentication,Reactjs,Firebase Authentication,我有一个可以登录的按钮。如果我单击该按钮,它将链接到主页(我使用了href)。但是我想通过firebase.auth.signin()检查电子邮件和密码是否正确。我尝试使用event.preventDefault(),但如果我等到firebase.auth回复后,不管发生什么情况,页面都会转到主页,那么它就不起作用了。我如何才能等到firebase.auth完成工作并正确执行默认设置 async c (e) { try { if(this.state.e

我有一个可以登录的按钮。如果我单击该按钮,它将链接到主页(我使用了href)。但是我想通过firebase.auth.signin()检查电子邮件和密码是否正确。我尝试使用event.preventDefault(),但如果我等到firebase.auth回复后,不管发生什么情况,页面都会转到主页,那么它就不起作用了。我如何才能等到firebase.auth完成工作并正确执行默认设置

 async c (e) {
        try {
            if(this.state.email == null || this.state.password == null){
                e.preventDefault();
            }

            var config = {
                apiKey: "",
                authDomain: "",
                databaseURL: "",
                projectId: "",
                storageBucket: ".",
                messagingSenderId: ""
            };

            !firebase.apps.length ? firebase.initializeApp(config) : firebase.app();


            await firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.password)
            .then(function(user){

            },
            function(error) {
                e.preventDefault();
            });   
        } catch (error) {
            e.preventDefault();
        }
    }                                                                                                                            <a href="/mainpage"><a>                              
(更多信息) 我尝试了异步,但没有成功。我做了下面的事情。由于时间问题,preventDefault()似乎无法正常工作。似乎如果href在几秒钟内没有收到答复,它会在fire.auth返回消息之前跳转到下一页:(

 async c (e) {
        try {
            if(this.state.email == null || this.state.password == null){
                e.preventDefault();
            }

            var config = {
                apiKey: "",
                authDomain: "",
                databaseURL: "",
                projectId: "",
                storageBucket: ".",
                messagingSenderId: ""
            };

            !firebase.apps.length ? firebase.initializeApp(config) : firebase.app();


            await firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.password)
            .then(function(user){

            },
            function(error) {
                e.preventDefault();
            });   
        } catch (error) {
            e.preventDefault();
        }
    }                                                                                                                            <a href="/mainpage"><a>                              
异步c(e){ 试一试{ if(this.state.email==null | | this.state.password==null){ e、 预防默认值(); } 变量配置={ apiKey:“, authDomain:“”, 数据库URL:“”, 投射:“, storageBucket:“.”, messagingSenderId:“ }; !firebase.apps.length?firebase.initializeApp(配置):firebase.app(); 等待firebase.auth().signInWithEmailAndPassword(this.state.email,this.state.password) .then(功能(用户){ }, 函数(错误){ e、 预防默认值(); }); }捕获(错误){ e、 预防默认值(); } }
您可以使用异步/await实现此目的

 async c (e) {
        try {
            if(this.state.email == null || this.state.password == null){
                e.preventDefault();
            }

            var config = {
                apiKey: "",
                authDomain: "",
                databaseURL: "",
                projectId: "",
                storageBucket: ".",
                messagingSenderId: ""
            };

            !firebase.apps.length ? firebase.initializeApp(config) : firebase.app();


            await firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.password)
            .then(function(user){

            },
            function(error) {
                e.preventDefault();
            });   
        } catch (error) {
            e.preventDefault();
        }
    }                                                                                                                            <a href="/mainpage"><a>                              

您可以使用异步/await实现此目的

 async c (e) {
        try {
            if(this.state.email == null || this.state.password == null){
                e.preventDefault();
            }

            var config = {
                apiKey: "",
                authDomain: "",
                databaseURL: "",
                projectId: "",
                storageBucket: ".",
                messagingSenderId: ""
            };

            !firebase.apps.length ? firebase.initializeApp(config) : firebase.app();


            await firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.password)
            .then(function(user){

            },
            function(error) {
                e.preventDefault();
            });   
        } catch (error) {
            e.preventDefault();
        }
    }                                                                                                                            <a href="/mainpage"><a>                              

这应该返回一个承诺,您可以在then()中进行重定向,或者使用Sneha指出的async/await。这应该返回一个承诺,您可以在then()中进行重定向,或者使用Sneha指出的async/await
 async c (e) {
        try {
            if(this.state.email == null || this.state.password == null){
                e.preventDefault();
            }

            var config = {
                apiKey: "",
                authDomain: "",
                databaseURL: "",
                projectId: "",
                storageBucket: ".",
                messagingSenderId: ""
            };

            !firebase.apps.length ? firebase.initializeApp(config) : firebase.app();


            await firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.password)
            .then(function(user){

            },
            function(error) {
                e.preventDefault();
            });   
        } catch (error) {
            e.preventDefault();
        }
    }                                                                                                                            <a href="/mainpage"><a>