javascript - 登录显示不需要的警报
问题描述
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");
}
我在这里有这段代码,当输入有效的电子邮件和密码时,用户按下按钮并注册用户。但是当用户输入无效数据时,警报(“已注册”)会与错误消息一起显示,有没有办法避免这种情况?
解决方案
如所写,“已注册”警报会在 createUser 完成之前立即运行,无论它是否成功。使用 Promise 的方法是将成功完成后应该执行的代码放在一个then
块中,并将错误处理——正如你所拥有的——放在一个 catch...
const promise = auth.createUserWithEmailAndPassword(email.value, password.value);
promise.then(() => alert("Signed Up")).catch(e => alert(e.message));
推荐阅读
- python - Ansible 动态库存 python 解释器问题
- python - 使用所有可能的行对评估函数
- ios - STHTTPRequest 返回 null
- http - 密码在fiddler、wireshark中显示
- sqlite - 在 SQLite 中,如何删除一个表中与另一表中的索引相对应的行
- sql - SQL - 当产品存在时,将其对应的值填充到分区内的所有行
- python - 如何为 pandas 数据框中的不同组分配唯一 ID?
- iteration - gnuplot:从同一输出上的不同数据文件创建多个箱线图
- c++ - 如何解决“错误:对‘abs’的调用不明确”
- php - 如何让 SimpleSAMLphp 1.17.2 让用户退出我的网站?它在 SSP 1.16.3 中工作