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”)会与错误消息一起显示,有什么办法可以避免这种情况?
如所写,无论是否成功,都会在createUser完成之前立即运行“注册”警报。使用promise的方法是将应在成功完成后执行的代码放在一个then
块中,并将错误处理(如你所愿)放在一个陷阱中。
const promise = auth.createUserWithEmailAndPassword(email.value, password.value);
promise.then(() => alert("Signed Up")).catch(e => alert(e.message));