2016-10-20 13 views
3

FirebaseError has a "code" propertyですが、約束のキャッチメソッドでそれをどのように読み取っていますか?以下は、の活字体エラースロー:コードのプロパティにアクセスするにはProperty 'code' does not exist on type 'Error'.AngularFire/TypeScriptでFirebaseErrorの "code"プロパティにどのようにアクセスしますか?

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); 
}); 

答えて

6

を、あなたはそうのように、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); 
}); 
+0

ありがとう!あなたは最新のアングラーファイアーの詳細なドキュメントをどこから見つけることができますか?それは一緒に働くお尻の痛みです – Ruben

2

ながらPatrickmcdの回避策働くでしょう。それは理想的な解決策ではありません。 Firebaseオブジェクトをインポートしてエラーオブジェクトに正しいタイプを持たせることに頼るべきではありません。これは、Angular Fire Moduleのポイントを破ります。 また、アプリケーションに不必要な大量のバルクを追加します。 がここにバグレポートを参照してください。https://github.com/angular/angularfire2/issues/666

を私の回避策はFirebaseライブラリをインポートする必要はありませんブラケット表記と文字列リテラルを使用してベータ7に

を修正される予定です。

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; 

    }); 

・ホープこのことができます以下の例を参照してください!

関連する問題