Express.js + Facebook

时间:2015-04-29 10:18:42

标签: node.js facebook express

我在尝试使用此Express应用程序时遇到问题。我想在Facebook getLoginStatus函数中调用(response.status ===' connected')if分支中的函数。这是代码:

    (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'));
        };
      }]);
    })();

登录功能由div内的ng-init指令调用。 加载我的HTML页面时,我收到错误" TypeError:this.getFriends不是函数"。也许问题是我在$ window中的函数define中调用this.getFriends()。我怎样才能让事情奏效?

提前谢谢你, 弗朗西斯

编辑:我想我知道问题是"这个"关键字,但我怎么能让它没有?

1 个答案:

答案 0 :(得分:1)

这很简单。在FB.getLoginStatus内,this指向其他内容。解决方法:

(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'));
        };
      }]);
    })();