2016-09-01 4 views
0

クラス内の関数の呼び出し/アクセスに問題があり、外部のjsライブラリ/コードを呼び出す関数からのサービス関数...角度2の成分facebook sdk api関数からクラス関数を参照できません

FB <で「この」キーワードを使用して、クラス変数にアクセスすることは - Facebookはここでは、コード

statusChangeCallback(resp: any) { 
     if (resp.status === 'connected') { 
      this.access_token = resp.authResponse.accessToken; // this variable gets the correct value in it 

      FB.api('/me?fields=name,email', function (resp: any) { 
       this.email = resp.email; // this variable gets the correct value in it 

       if (this.email !== '' && this.access_token !== '') { 
        console.log('under if statement'); 
        var auth = {}; 
        auth['accesstoken'] = this.access_token; 
        auth['emailid'] = this.email; 

        console.log(auth); 

        this.send_registeration(auth); // this function throws ERROR 
        // this.http.fb_register(this.email, this.access_token); 
       } 
      }, { scope: 'email,public_profile' }); 
     } else if (resp.status === 'not_authorized') { 

     } else { 

     } 
    } 

だログインしているユーザー

の値を伝え取得するFacebookのSDKの機能ですここにエラーがありますnは

zone.js:260 Uncaught TypeError: this.send_registeration is not a function 

がここ

import {Component, OnInit, Output} from '@angular/core'; 
import {ROUTER_DIRECTIVES, Router} from '@angular/router-deprecated'; 
import { HttpService } from '../../Service/http.service'; 

declare const FB: any; 

@Component({ 
    selector: 'facebook-login', 
    template: ` 
    <div> 
     <button class="btn btn-facebook" (click)="onFacebookLoginClick()"> 
      <i class="fa fa-facebook"></i>Sign in with Facebook 
     </button> 
    </div> 
    `, 
    providers: [HttpService], 
    directives: [ROUTER_DIRECTIVES] 
}) 

export class FacebookLoginComponent implements OnInit { 

    access_token: string = ''; 
    email: string = ''; 


    constructor(private http: HttpService) { 
     FB.init({ 
      appId: '****APP ID **********', 
      cookie: false, // enable cookies to allow the server to access 
      // the session 
      xfbml: true, // parse social plugins on this page 
      version: 'v2.5' // use graph api version 2.5 
     }); 
    } 

    onFacebookLoginClick() { 
     FB.login(this.statusChangeCallback); 
    } 

    statusChangeCallback(resp: any) { 
     if (resp.status === 'connected') { 
      this.access_token = resp.authResponse.accessToken; 
      // var self = this; 
      FB.api('/me?fields=name,email', (resp: any) => { 
       this.email = resp.email; 

       if (this.email !== '' && this.access_token !== '') { 

        var auth = {}; 
        auth['accesstoken'] = this.access_token; 
        auth['emailid'] = this.email; 

        console.log(auth); 

        this.send_registeration(auth); //throws Error 
        // this.http.fb_register(this.email, this.access_token); // this Service function also throws Error just the same way 
       } 
      }, { scope: 'email,public_profile' }); 

     } else if (resp.status === 'not_authorized') { 

     } else { 

     } 
    } 

    send_registeration(auth: any) { 
     this.http.postRequest(auth, 'fbinvestors') 
      .subscribe(
      data => { 
       console.log('Server respond is '); 
       console.log(data); 
      } 
      ); 
    } 
} 

をチェックアウトするには、完全なコンポーネントのコードがあります。ここ更新機能はのChrome ... FacebookのSDKのコールバックdoesntのは、先に述べたように、以前のようです...しかし、問題はまだ

を終了します
statusChangeCallback(resp: any) { 

     if (resp.status === 'connected') { 
      this.access_token = resp.authResponse.accessToken; 

     FB.api('/me?fields=name,email,first_name,last_name,age_range,gender,picture', (resp: any) => { 
       this.email = resp.email; 
     }, { scope: 'email,public_profile' }); 


     } 

     var self = this; 
     setTimeout(function() { 
      if (this.email !== '' && this.access_token !== '') { 
       console.log('under if statement'); 
       var auth = {}; 
       auth['accesstoken'] = this.access_token; 
       auth['emailid'] = this.email; 

       console.log(auth); // show variable output as required 
// no problem till here 

      } 
      self.http.postRequest(auth, 'fbinvestors') // this line throws error as shown below 
       .subscribe(
       data => { 
        console.log('Server respond is '); 
        console.log(data); 
       } 
       ); 

     }, 7000); 
    } 

新しいエラーが古いものに似ている...しかし、今、そのサービスのメソッドを呼び出していない - エラー示すが

です
TypeError: Cannot read property 'postRequest' of undefined 

UPDATE:ラインself.http.postRequest(認証、「fbinvestors」)に...自己は基本的に定義されていません...私は '(これをクラスのスコープを渡すことができ 1.あれば今、私はこの問題を解決します「)このコールバック関数 2のパラメータとして、私は、コールバック関数の配列を提供できる場合だけではなく、1コールバック関数の

助けてください - 私は今、2日から解決を取得するために、これをしようとしています....

+0

どこでsend_registeration関数を定義しましたか? statusChangeCallbackと同じスコープで? –

+0

私は同じクラスでそれを定義しています... after statusChangeCallback関数 –

+0

おそらく、facbook APIは何とかコールバックを変更します。 @talkdirtyの解決策を試しましたか? –

答えて

0
function (resp: any) { 

する必要があります
(resp: any) => 

それ以外の場合は、this.は現在のコンポーネントインスタンスを指しません。 も参照してくださいhttps://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Functions/Arrow_functions

+0

私はこれを試しましたが、その働きは同じエラーです –

+0

想像できません。より多くのコードを提供してください( 'send_registeration'メソッドとそれを囲むクラスを表示してください)。 –

+0

は完全なコンポーネントコード –

0

スレッドthis excellent Stackoverflowをご覧ください。あなたが抱えている問題とその解決方法について説明します。 コールバック関数内のthisは、間違ったコンテキストを参照しています。最終的には、クラスではなく、Facebook APIコードのどこかから呼び出されるためです。したがって、このthisを使って他のクラス関数にアクセスすることはできません。

ありがたいことに、これを解決するには、非常に簡単です:

... 
var self = this; 
FB.api('/me?fields=name,email', function (resp: any) { 
    ... 
    self.send_registeration(auth); 

temorary変数にthisを入れて、コールバックの内部を使用すると、右のコンテキストを参照します。

+0

で質問を更新しました。私は以前これを試していました...あなたは私が再び試したと言いましたが、 –

0

私は、再帰コールバックを経由してFBからの応答を待っているこの

onFacebookLoginClick() { 
      var self = this; 
      FB.login(this.statusChangeCallback); 
      var callback = setTimeout(function() { 
       if (this.email !== '' && this.access_token) { 
        console.log('send the http request from here'); 
        var auth = {}; 
        auth['emailid'] = this.email; 
        auth['accesstoken'] = this.access_token; 
        self.httpTest.postRequest(auth, 'fbinvestors') 
         .subscribe(
         data => { 
          console.log('Server respond is '); 
          console.log(data); 
         } 
         ); 
       } else { 
        this.callback(); // gave a recursion untill post is not done. 
       } 

      }, 2000); 
     } 
statusChangeCallback(resp: any) { 

     if (resp.status === 'connected') { 
      this.access_token = resp.authResponse.accessToken; 

      // var self = this; // as shown on online to solve the 'this' keyword problem 
      console.log(this.httpTest + ' is the service I want to access'); 
      FB.api('/me?fields=name,email,first_name,last_name,age_range,gender,picture', 
       (resp: any, httpService: any = this.httpTest) => { 
        this.email = resp.email; 
       }); 
     } else if (resp.status === 'not_authorized') { 

     } else { 

     } 
    } 

に元の関数に

onFacebookLoginClick() { 
     FB.login(this.statusChangeCallback); 
    } 

statusChangeCallback(resp: any) { 

     if (resp.status === 'connected') { 
      this.access_token = resp.authResponse.accessToken; 

     FB.api('/me?fields=name,email,first_name,last_name,age_range,gender,picture', (resp: any) => { 
       this.email = resp.email; 
     }, { scope: 'email,public_profile' }); 


     } 

     var self = this; 
     setTimeout(function() { 
      if (this.email !== '' && this.access_token !== '') { 
       console.log('under if statement'); 
       var auth = {}; 
       auth['accesstoken'] = this.access_token; 
       auth['emailid'] = this.email; 

       console.log(auth); // show variable output as required 
// no problem till here 

      } 
      self.http.postRequest(auth, 'fbinvestors') // this line throws error as shown below 
       .subscribe(
       data => { 
        console.log('Server respond is '); 
        console.log(data); 
       } 
       ); 

     }, 7000); 
    } 

を変更するには、明らかに良いアイデアではありません...しかし、今のところ、それはすることで解決しましたあなたが正しい解決策が見つからなくなるまで問題が発生する

関連する問題