简体   繁体   English

Express.js + Facebook

[英]Express.js + Facebook

I'm experiencing problems trying to get this Express app working. 我在尝试使此Express应用正常工作时遇到了问题。 I want to call a function in the (response.status === 'connected') if branch, inside the Facebook getLoginStatus function. 我想在Facebook getLoginStatus函数内部的if分支中调用(response.status ==='connected')中的函数。 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. 登录函数由div中的ng-init指令调用。 When load my HTML page I get the error "TypeError: this.getFriends is not a function". 加载我的HTML页面时,出现错误“ TypeError:this.getFriends不是函数”。 Maybe the problem is that I call this.getFriends() inside a function define in $window. 也许问题是我在$ window中定义的函数内调用了this.getFriends()。 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? 编辑:我想我知道问题是“ this”关键字,但是如果没有它,我如何使它工作呢?

That's simple. 很简单 Inside the FB.getLoginStatus , this is pointing to something else. FB.getLoginStatus内部, this指向其他内容。 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'));
        };
      }]);
    })();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM