2017-06-05 110 views
0

我有一個錯誤離子2:未定義不是一個對象

未定義沒有(評價 'this.email = '123'')的對象

這是爲什麼?

我離子2碼控制器:

email:string = '' ; 

facebook_login() { 

    this.fb.login(['public_profile', 'email']) 
     .then((res: FacebookLoginResponse) => { 

      this.fb.api("/me?fields=name,email", []).then(function(user) { 

       this.email = '123' ; 

      }) ; 

    }).catch(e => { 
     alert('Error login') ; 
    }); 

} 

回答

2

使用箭頭功能回調() => {}

this將指向函數對象而不是您的示例中的類。

this.fb.api("/me?fields=name,email", []).then(function(user) { 

       this.email = '123' ; 

      }) ; 

更改上述給:

this.fb.api("/me?fields=name,email", []).then((user) => { 

       this.email = '123' ; 

      }) ; 
相關問題