首页 > 解决方案 > 登录显示不需要的警报

问题描述

    function signUp(){
    
    var email = document.getElementById("email");
    var password = document.getElementById("password");
    
    const promise = auth.createUserWithEmailAndPassword(email.value, password.value);

            promise.catch(e => alert(e.message));
    
    
    
        alert("Signed Up");


}

我在这里有这段代码,当输入有效的电子邮件和密码时,用户按下按钮并注册用户。但是当用户输入无效数据时,警报(“已注册”)会与错误消息一起显示,有没有办法避免这种情况?

标签: javascriptfirebaseauthentication

解决方案


如所写,“已注册”警报会在 createUser 完成之前立即运行,无论它是否成功。使用 Promise 的方法是将成功完成后应该执行的代码放在一个then块中,并将错误处理——正如你所拥有的——放在一个 catch...

const promise = auth.createUserWithEmailAndPassword(email.value, password.value);
promise.then(() => alert("Signed Up")).catch(e => alert(e.message));

推荐阅读