【问题标题】:Ionic Firebase Auth: Cannot read error codeIonic Firebase 身份验证:无法读取错误代码
【发布时间】:2018-01-07 07:34:22
【问题描述】:

我正在使用 Ionic 3 / Angular 4 / Firebase 构建应用程序。以下代码正确验证用户身份并且登录正常。

但我无法读取error.code,即使它在console.log(error) 中。

这是我的代码:

firebase.auth().signInWithEmailAndPassword(this.user.email, this.user.password)
        .then((result) => {
            // show toast
            this.toast.show("Login was successful");
            // set user to storage
            this.storage.set("user", JSON.stringify(result))
            // restore form submitted state
            this.submitted = false;
            // set nav root to Dashboard
            this.navCtrl.setRoot("DashboardPage");
        })
        .catch((error) => {
            console.error(error);

            let errorCode = error.code // THIS CAN'T BE READ
        });

VS Code 建议的错误对象的属性为error.messageerror.nameerror.stack

这是控制台日志:

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

更新

投射到any 有效,但我不认为这是它应该做的方式。

.catch((error: any) => {

【问题讨论】:

    标签: angular firebase ionic-framework firebase-authentication


    【解决方案1】:

    尝试给error 类型firebase.FirebaseError,像这样:

    .catch((error:firebase.FirebaseError) => {
        console.error(error);
        let errorCode = error.code // THIS CAN'T BE READ
    });
    

    【讨论】:

    • 感谢您的回答。不幸的是,我收到了一个新错误:Argument of type '(error: Error) => void' is not assignable to parameter of type '(a: Error) => any'. Types of parameters 'error' and 'a' are incompatible. Type 'Error' is not assignable to type 'firebase.auth.Error'. Property 'code' is missing in type 'Error'.
    • 是的,我遇到了同样的错误!但仅在包含我的服务的文件中,我已在其中导入了 firebase。
    • 改用firebase.FirebaseError
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2022-06-13
    • 1970-01-01
    • 2019-09-14
    • 2016-10-30
    • 2018-01-25
    • 2016-04-22
    相关资源
    最近更新 更多