Javascript catch中的Async Wait only输出错误消息
我有以下异步函数:Javascript catch中的Async Wait only输出错误消息,javascript,firebase,error-handling,firebase-authentication,Javascript,Firebase,Error Handling,Firebase Authentication,我有以下异步函数: async function register() { try { if (password1 !== password2) { throw "Passwords do not match!"; } await firebase.auth().createUserWithEmailAndPassword(email, password1) } } catch (err) { document.que
async function register() {
try {
if (password1 !== password2) {
throw "Passwords do not match!";
}
await firebase.auth().createUserWithEmailAndPassword(email, password1)
}
} catch (err) {
document.querySelector("#message").innerHTML = err;
return;
}
第一个密码匹配检查用户两次输入的密码是否相同。如果是,则使用Firebase创建帐户。但是Firebase的
createUserWithEmailAndPassword
函数返回一个error
对象,该对象包含error.code
和error.message
。我只想显示error.message
并忽略error.code
。可以在尝试之后使用公共的catch
块执行此操作吗?或者我必须为createUserWithEmailAndPassword
编写一个特定的catch
块来忽略错误代码?您可以尝试在您的应用程序中匹配firebase的结构
抛出错误条件使用
throw { message: 'Passwords do not match!' };
然后始终将错误作为对象处理
在这两种情况下
编辑:
多亏了Bergi,您还可以使用这种更好的方法
throw new Error('Passwords do not match!);
它还将创建一个具有消息属性的对象您可以尝试在 抛出错误条件使用
throw { message: 'Passwords do not match!' };
然后始终将错误作为对象处理
在这两种情况下
编辑:
多亏了Bergi,您还可以使用这种更好的方法
throw new Error('Passwords do not match!);
如果您抛出新错误(“密码不匹配”),它还将创建一个具有消息属性的对象如果
抛出新错误(“密码不匹配”),则err
将是一个对象,并具有.message
属性代码>错误将是一个对象,并且有一个。消息属性,或者更好的是,抛出新错误(“密码不匹配”)
或甚至抛出新密码不匹配错误()
谢谢,我知道遗漏了什么:DOr甚至更好,抛出新错误(“密码不匹配”)
甚至抛出新密码不匹配错误()
谢谢,我知道缺少了一些东西:D