2

i try to call a service's function(loginFb in auth.service.ts) from a component (fb.component.ts). It seems that i imported everything and init the service. but still getting a 'loginfb' undefined error. my auth.service.ts:

import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';

@Injectable()
export class AuthService {

 constructor(private http: Http) {}

 loginFb(uid, accessToken): boolean{
   let headers = new Headers();
   headers.append('Content-Type', 'application/json');
   let body = JSON.stringify({"uid":uid,"accessToken": accessToken});
   this.http.post('http://localhost:3002/signinfb',body,{headers:headers})
   .toPromise()
   .then(response => {
      localStorage.setItem('id_token', response.json().id_token);
      return true;
    })
  .catch(this.handleError);
 return false;
}

private handleError(error: any): Promise<any> {
   console.error('An error occurred', error);
   return Promise.reject(error.message || error);
  }
 }

in my fb.component.ts:

  import { Component, OnInit } from '@angular/core';
  import { Router } from "@angular/router";
  import { AuthService } from './auth.service';


  declare const FB:any;

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

  export class FacebookLoginComponent implements OnInit{

    constructor(private authService: AuthService) {
    }
    ngOnInit() {
      FB.init({
            appId      : '234244113643991',
            cookie     : false,
            xfbml      : true,  
            version    : 'v2.7'
        });
    }

    statusChangeCallback(response) {
        if (response.status === 'connected') {
          let uid = response.authResponse.userID;
          let accessToken = response.authResponse.accessToken;
          // window.alert(uid+"|"+accessToken);
          if (this.authService.loginFb(uid,accessToken)){
            window.alert("GOOD!");
          }else{

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

        }else {

        }
    }

    onFacebookLoginClick() {
      FB.login(this.statusChangeCallback, 
      {scope: 'public_profile,email,user_friends,'});
    }
  }

I am getting:

  Subscriber.ts:241 Uncaught TypeError: Cannot read property 'loginFb' of undefined(…)

Can someone help me with this problem? thanks!

1 Answer 1

1

You need to bind this to the proper context with statusChangeCallback

FB.login(this.statusChangeCallback.bind(this),

or make statusChangeCallback an arrow function

statusChangeCallback = (response) => {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.