2017-05-18 24 views
0

私はCordovaのFacebookプラグインを使用してユーザー名/電子メール/ etcを取得しています(これはうまくいきます)。私は機能を作成してDBコードへの投稿をテストし、テストボタンに結びつけています。しかし、私はポスト2-dbコードをfb-loginコード内に置くと、http-postで正確に実行を停止します。私はにconsole.log(STEP1)を行うと、細かいことがわかり、私ははconsole.log(STEP2)を見ることはありません - 以下の私のコードを参照してください。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への電話で例外が発生したことを示しています。たとえば、thisまたはthis.httpは定義できません。コンソールウィンドウを調べて、関連するものがあるかどうかを確認します。

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関数呼び出しを渡す取得していない理由についての洞察力を与える必要があります。

例外をキャッチする方法は、try/catchブロックを使用することです。

2

このコードを関数に入れると、 "this"がスコープを変更します。

this.http.post 

"this"は変更したばかりの実行コンテキストを指し、「http」は付けられていません。またはstrictを使用している場合は、未定義となります。

新しいes6矢印機能を試してみてください。彼らは静的な "this"を提供します。それとも単にで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 
}); 
関連する問題