I am developing a website that needs to be registered with a Facebook account. I am using Angular 2 and of course TypeScript. It works, but not quite what I wanted. I cannot return user information.
Go to the code:
import {Component} from 'angular2/core'; import {Main} from './pages/main/main'; declare const FB: any; @Component({ selector: 'my-app', templateUrl: 'app/app.html', directives: [Main] }) export class AppComponent implements OnInit { token: any; loged: boolean = false; user = { name: 'Hello' }; constructor() { } statusChangeCallback(response: any) { if (response.status === 'connected') { console.log('connected'); } else { this.login(); } } login() { FB.login(function(result) { this.loged = true; this.token = result; }, { scope: 'user_friends' }); } me() { FB.api('/me?fields=id,name,first_name,gender,picture.width(150).height(150),age_range,friends', function(result) { if (result && !result.error) { this.user = result; console.log(this.user); } else { console.log(result.error); } }); } ngOnInit() { FB.getLoginStatus(response => { this.statusChangeCallback(response); }); } }
Basically, when the page loads, I check if the user is registered on Facebook, if not, I call the login method. The me method is used to retrieve information about users, such as name, first name, etc. When I logged into the conditions browser console, print the following line:
Object {id: "666", name: "Paulo Henrique Tokarski Glinski", first_name: "Paulo", gender: "male", picture: Object…}
Everything is good! But I want to get this object and put it in a User object! Something like that:
method :
this.user = result; console.log(this.user);
But the user simply exists inside the method. If I print it outside, it will not return anything.
I just want to print the username, etc. On the site page. I did pretty much the same with Angular JS and worked well.
You are welcome! Help me!
javascript angular facebook typescript
Paulo Tokarski Glinski
source share