【问题标题】:How do you access the "code" property of a FirebaseError in AngularFire / TypeScript?如何在 AngularFire / TypeScript 中访问 FirebaseError 的“代码”属性?
【发布时间】:2017-03-02 15:17:47
【问题描述】:

FirebaseError has a "code" property,但是如何在 promise 的 catch 方法中读取它?以下会引发 TypeScript 错误:Property 'code' does not exist on type 'Error'.

this.af.database
.object(`/some/path`)
.set(newObj)
.then(data => {
  console.log('success');
})
.catch(err => {
  // Property 'code' does not exist on type 'Error'.
  console.log(`code`, err.code);
});

【问题讨论】:

    标签: angular typescript firebase angularfire


    【解决方案1】:

    要访问 code 属性,您需要导入 firebase 并将您的错误指定为 firebase.FirebaseError 类型,如下所示:

    import { AngularFire } from 'angularfire2';
    import firebase from 'firebase';
    
    ...
    
    constructor(
      private af: AngularFire
    ) {}
    
    ...
    
    this.af.database
    .object(`/some/path`)
    .set(newObj)
    .then(data => {
      console.log('success');
    })
    .catch( (err: firebase.FirebaseError) => {
      // Give your error the firebase.FirebaseError type and
      // you'll have access to all the FirebaseError properties
      console.log(`code`, err.code);
      console.log(`message`, err.message);
      console.log(`name`, err.name);
      console.log(`stack`, err.stack);
    });
    

    【讨论】:

    • 谢谢!在哪里可以找到最新 angularfire 的更多文档?和他一起工作很痛苦
    【解决方案2】:

    虽然 Patrickmcd 的解决方法会起作用。这不是一个理想的解决方案。您不应该依赖导入 firebase 对象来在错误对象上拥有正确的类型。这违背了 Angular Fire 模块的观点。 它还在您的应用程序中增加了许多不必要的体积。 请在此处查看错误报告:https://github.com/angular/angularfire2/issues/666

    计划在 beta 7 中修复

    使用括号表示法和字符串文字 我的解决方法不需要您导入 Firebase 库。

    看下面的例子

     this.af.auth.login({
          email: this.userEmail,
          password: this.userPassword
        }).catch(error => {
          // Returns the firebase Error Code documented here https://firebase.google.com/docs/reference/js/firebase.auth.Error
          console.log(error['code']);
          // Returns the fire base message associated with the Error Code
          console.log(error['message']);
    
          // Show failed login validation
          this.loginFailed = true;
    
        }); 
    

    希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      另一种解决方法,您可以尝试从“@firebase/util”包中导入全局 FirebaseError 并使用类型保护进行检查,如下所示。

      import { FirebaseError } from '@firebase/util'
      
      try {
          // Some firebase functions
          await signInWithEmailAndPassword(auth, email, password)
      } catch (error: unknown) {
         if (error instanceof FirebaseError) {
            console.error(error.code)
         }
      }
      

      【讨论】:

      • 注意:我使用的是firebase sdk 9.x版
      【解决方案4】:

      如果您使用单个 Firebase 模块,每个模块都有一个错误类型。

      import { FirestoreError } from 'firebase/firestore'
      
      .catch( (err: FirestoreError) => {
        // Give your error the firebase.firestore.Error type and
        // you'll have access to all the Error properties
        console.log(`code`, err.code);
        console.log(`message`, err.message);
        console.log(`name`, err.name);
        console.log(`stack`, err.stack);
      });
      

      【讨论】:

        猜你喜欢
        • 2019-07-27
        • 1970-01-01
        • 2013-10-17
        • 1970-01-01
        • 2011-12-19
        • 1970-01-01
        • 2022-11-06
        • 2014-10-17
        • 2018-10-30
        相关资源
        最近更新 更多