Harshit Mulchandani
Harshit Mulchandani

Reputation: 101

Cannot use api method in ng2-facebook-sdk for angular2 to fetch user data

Has anyone used the component named ng2-facebook-sdk? If so does anyone know how I would go about fetching user data from the graph api using the .api method. Here's the link to the npm package readme guide.I could not understand how to use it - https://www.npmjs.com/package/ng2-facebook-sdk. My problem is that i have successfully logged in the user but i am not able to write the .api function which returns a Promise object

Currently my code looks like this...

import { Component } from "@angular/core";
import {FacebookService, FacebookInitParams} from "ng2-facebook-sdk";
import { Http } from "@angular/http";
import * as http from "selenium-webdriver/http";

 @Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css']
 })
 export class AppComponent {

 name = "";
 isUser = false;
  no = false;

 constructor(private _facebookService: FacebookService,private http:Http) {
 let fbParams: FacebookInitParams = {
  appId: 'xxxxxxxxxxxxxxx',
  xfbml: true,
  version: 'v2.6'
};

  this._facebookService.init(fbParams);
}


login() {
  this._facebookService.login().then((response) => {
   this.getUserData();
   console.log(response);

 });
}

getUserData(){

  this._facebookService.api('/me', http,(res) {

  this.name = res.name;
   this.isUser = true;
  });

}
}

Upvotes: 3

Views: 913

Answers (1)

Harshit Mulchandani
Harshit Mulchandani

Reputation: 101

I figured it out myself. I was not handling the Promise object response correctly. This should have been the login function ->

login() {
this._facebookService.login().then((response) => {
  var promise = this._facebookService.api('/me');
  promise.then((res)=> {
    this.id = res.id;
      this.name = res.name;
      this.isUser = true ;
      console.log(res);

    });
  console.log(response);

});

}

Upvotes: 1

Related Questions