【发布时间】:2018-08-12 07:24:14
【问题描述】:
在使用 Angular5 和 firebase 作为数据库时,我正在尝试从 createUserWithEmailAndPassword(email, password) 创建一个新用户。
但是当我输入一个已经存在的电子邮件(用于测试)时,该函数将返回 HTTP POST 错误以及(不是预期的)错误对象(预期的)。
我尝试使用 .catch() 捕获错误,但 HTTP POST 错误仍在使用我的 API 密钥连同 firebase URL 一起打印到控制台。
这是我在 AuthService 打字稿类中的代码:
signupUser(username: string, password: string) {
firebase.auth().createUserWithEmailAndPassword(username, password)
.catch(function(err) {
const errorCode = err.code;
const errorMsg = err.message;
console.log('Error code: ', errorCode);
console.log('Error Message: ', errorMsg);
console.log('Error: ', err);
});
}
这里是调用上述代码的地方:
onSignup(form: NgForm) {
const username: string = form.value.username;
const password: string = form.value.password;
try {
this.authService.signupUser(username, password);
} catch (err) {
console.log('try-catch called ');
}
}
以下是控制台上显示的内容:
请建议我在这里做错了什么,因为我不希望将 HTTP POST 消息打印到显示我的 API_KEY 的控制台。
【问题讨论】:
-
不存在错误吗?
Error code: auth/email-already-in-use和Error Message: The email address is already in use by another account.可以尝试删除多次注册同一个邮箱的用户? -
是的,错误正在显示,但我想知道如何捕捉显示在这些错误上方的
HTTP POST 400 error。
标签: angular firebase error-handling http-post firebase-authentication