1

I'm experiencing problems trying to get this Express app working. I want to call a function in the (response.status === 'connected') if branch, inside the Facebook getLoginStatus function. Here's the code:

    (function(){
      var app = angular.module('AppProva', ['ngResource']);
      app.controller('friendFetcherCtrl', ['$window', function($window){
        this.getFriends = function(){
          console.log('GETFRIENDS()');
        };
        this.login = function() {
          console.log('LOGIN()');
          $window.fbAsyncInit = function() {
            FB.init({ 
              appId: '****************',
              xfbml: true,
              version : 'v2.3'
            });
            FB.getLoginStatus(function(response) {
              if (response.status === 'connected') {
                console.log('Logged in.');
                this.getFriends();
                /*Facebook graph query*/
              }
              else {
                FB.login(function() { /* Do something */ }, { scope : 'user_friends, public_profile' });
              }
            });
          };
          (function(d, s, id) {
            var js, fjs = d.getElementsByTagName(s)[0];
             if (d.getElementById(id)) {
               return;
             }
             js = d.createElement(s);
             js.id = id;
             js.src = "//connect.facebook.net/en_US/sdk.js";
             fjs.parentNode.insertBefore(js, fjs);
          }(document, 'script', 'facebook-jssdk'));
        };
      }]);
    })();

The login function is called by the ng-init directive inside a div. When load my HTML page I get the error "TypeError: this.getFriends is not a function". Maybe the problem is that I call this.getFriends() inside a function define in $window. How can I get things working?

Thank you in advance, Francesco

EDIT: I think I know the probles is the "this" keyword but how can I make it work without?

  • possible duplicate of [How does the "this" keyword work?](http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work) – Ben Fortune Apr 29 '15 at 10:19

1 Answers1

1

That's simple. Inside the FB.getLoginStatus, this is pointing to something else. Work-around:

(function(){
      var app = angular.module('AppProva', ['ngResource']);
      app.controller('friendFetcherCtrl', ['$window', function($window){
        this.getFriends = function(){
          console.log('GETFRIENDS()');
        };
        var self = this;
        this.login = function() {
          console.log('LOGIN()');
          $window.fbAsyncInit = function() {
            FB.init({ 
              appId: '****************',
              xfbml: true,
              version : 'v2.3'
            });
            FB.getLoginStatus(function(response) {
              if (response.status === 'connected') {
                console.log('Logged in.');
                self.getFriends(); //self will point to this in app.controller's function
                /*Facebook graph query*/
              }
              else {
                FB.login(function() { /* Do something */ }, { scope : 'user_friends, public_profile' });
              }
            });
          };
          (function(d, s, id) {
            var js, fjs = d.getElementsByTagName(s)[0];
             if (d.getElementById(id)) {
               return;
             }
             js = d.createElement(s);
             js.id = id;
             js.src = "//connect.facebook.net/en_US/sdk.js";
             fjs.parentNode.insertBefore(js, fjs);
          }(document, 'script', 'facebook-jssdk'));
        };
      }]);
    })();
Gabriel Tomitsuka
  • 1,061
  • 10
  • 24