2017-05-23 10 views
0

あなたが私を助けてくれることを願っています。私はfirebaseからコールバックを取得しようとしています。ここに私のコードは角4のファイヤーベース//関数がコールバックに見つかりません

createAccount() { 

     this.afAuth.auth.createUserWithEmailAndPassword(this.email, this.password) 

      .then(function (message) { 
       console.log(message); 
       this.setUserProfile(); 
      }) 
      .catch(function (e) { 
       console.log(e); 
      }); 


} 
setUserProfile() { 
    // some stuff 
}` 

私はコールバックでthis.setUserProfile()を呼び出してみます。問題は、コールバックにthis.setUserProfile()を設定しても、この関数が見つからない場合です。console.logは問題なく動作します。

答えて

2

arrow functionを使用してコンテキストを保持します。

this.afAuth.auth.createUserWithEmailAndPassword(this.email, this.password) 

     .then((message) => {  // <---- use error function here 
      console.log(message); 
      this.setUserProfile(); 
     }) 
     .catch(function (e) {  // <---- if you use "this" in error callback, change it to arrow function too 
      console.log(e); 
     }); 
+0

あなたはとても素晴らしいです –

関連する問題