2017-05-18 24 views
0

我使用科爾多瓦Facebook插件來獲取用戶名/電子郵件/等(工作正常),然後想要插入到我自己的數據庫。我已經通過創建一個函數來測試發佈到DB代碼,將它綁定到一個測試按鈕,並且工作正常。但是,當我將post-2-db代碼放在fb-login代碼中時,它會停止執行完全在我的http-post中。我做的console.log(步驟1),並看到很好,但我從來沒有看到的console.log(第二步) - 見下面我的代碼:Ionic2/Angular2不能調用函數內功能

// LOGIN TO FACEBOOK 
doFbLogin(){ 
    let permissions = new Array(); 
    let nav = this.navCtrl; 
    //the permissions your facebook app needs from the user 
    permissions = ["public_profile"]; 


    Facebook.login(permissions) 
    .then(function(response){ 
     let userId = response.authResponse.userID; 
     let params = new Array(); 

     //Getting name and gender properties 
     Facebook.api("/me?fields=name,gender,email", params) 
     .then(function(user) { 
     user.picture = "https://graph.facebook.com/" + userId + "/picture?type=large"; 
     //now we have the users info, let's save it in the NativeStorage 
     NativeStorage.setItem('user', 
     { 
      name: user.name, 
      gender: user.gender, 
      picture: user.picture, 
      email: user.email, 
     }) 
     .then(function(){ 
      // BEGIN INSERT USER TO DB 
      // postRequest() { 
      var headers = new Headers(); 
      headers.append("Accept", 'application/json'); 
      headers.append('Content-Type', 'application/json'); 
      let options = new RequestOptions({ headers: headers }); 
      let postParams = { 
       userName: user.name, 
       userEmail: user.email, 
       userImage: user.picture, 
      } 

      console.log("STEP 1"); 

// IT WORKS UP TO THIS POINT 

      this.http.post("http://example.com/post.php", postParams, options) 
       .subscribe(data => { 
       console.log(data['_body']); 
       }, error => { 
       console.log(error);// Error getting the data 
       }); 
      //} 
      // END DB INSERT CODE 

// I NEVER SEE THIS CONSOLE LOG BELOW 

      console.log("STEP 2"); 



      // User Reg complete, push them to app 
      nav.push(TabsPage); 
     }, function (error) { 
      console.log(error); 
     }) 
     }) 
    }, function(error){ 
     console.log(error); 
    }); 

    } 
} 

如果我拿出的代碼塊,一切工作正常。我非常喜歡編碼,我會很感激任何透徹的外行人員的解釋和協助。謝謝。

+2

如果您將您的問題看起來不像小說,更像是一段非常短的代碼,其中所有不相關的行被刪除,您將得到更好的答案。 – Ari

回答

2

由於stevenvanc指出,

.then((response)=>{ 

否則更換您

.then(function(response){ 

線的this將指的函數的實例

1

這表明您致電http.post會導致異常。例如,thisthis.http可能未被定義。檢查你的控制檯窗口,看看你是否看到任何與之相關的東西。

捕獲異常的方法是使用try/catch塊:

try { 
    this.http.post("http://example.com/post.php", postParams, options) 
      .subscribe(data => { 
      console.log(data['_body']); 
      }, error => { 
      console.log(error);// Error getting the data 
      }); 
    console.log("Successful call!"); 
} catch (error) { 
    console.log(error); 
} 

這應該給你一下,爲什麼執行不獲得通過http.post函數調用的洞察力。

2

如果您將該代碼放入函數中,您的「this」將更改範圍。

this.http.post 

「this」指的是執行上下文,您剛剛更改並且沒有附加「http」。或者如果你使用嚴格的話,它甚至會是未定義的。

嘗試使用新的es6箭頭功能。他們提供了一個靜態的「這個」。或者只是通過HTTP對象英寸

0

使用map得到response從服務器並傳遞到subscribe塊。

this.http.post("http://example.com/post.php", postParams, options).map((res: Response) => res.json() || {}) 
.subscribe((data) => { 
    console.log(data['_body']); 
}, error => { 
    console.log(error);// Error getting the data 
});