Warm tip: This article is reproduced from serverfault.com, please click

javascript-登录显示不需要的警报

(javascript - Login Displaying unwanted Alert)

发布于 2020-11-30 05:35:43
    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");


}

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

Questioner
nova3333
Viewed
11
danh 2020-11-30 13:40:32

如所写,无论是否成功,都会在createUser完成之前立即运行“注册”警报。使用promise的方法是将应在成功完成后执行的代码放在一个then块中,并将错误处理(如你所愿)放在一个陷阱中。

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