首页 > 解决方案 > 使用 Firebase auth (Web) 捕获电子邮件更新错误

问题描述

我正在通过重新验证用户身份然后更新用户来更新用户在 Firebase 中的电子邮件:

const user = firebase.auth().currentUser;
const credential = firebase.auth.EmailAuthProvider.credential('my-email@test.com', 'my-password');

user.reauthenticateWithCredential(credential)
    .then(user.updateEmail('my-new-email@test.com'))
    .then(() => {
      console.log('ok');
    })
    .catch((error) => {
      if (error.code === 'auth/wrong-password') {
        // ...
      } else if (error.code === 'auth/invalid-email') {
        // ...
      } else if (error.code === 'auth/email-already-in-use') {
        // ...
      }
    });

它可以工作,但我无法捕捉到该user.updateEmail部分引发的错误。由于某些原因,该user.reauthenticateWithCredential部分的错误是caugth。例如,如果我输入了错误的密码auth/wrong-password,我的块中的代码代码就会出错catch((error) => {},但是如果我输入了无效的电子邮件,我只会在控制台中看到以下消息:

Uncaught 
L {code: "auth/invalid-email", message: "The email address is badly formatted."}

标签: javascriptfirebasefirebase-authentication

解决方案


Nervermind 我是个白痴:

.then(user.updateEmail('my-new-email@test.com'))

应该

.then(() => user.updateEmail('my-new-email@test.com'))

推荐阅读