简体   繁体   English

访问FB.login函数内部的变量

[英]Accessing variable inside of FB.login function

Please take a look at this function: 请看一下这个功能:

loginButton.on('click', function(e) {
    e.preventDefault();
    FB.login(function(response) {
        var gender;
        var events = [];
        if (response.status === 'connected') {
            userID = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken;
            FB.api('/me/groups', {access_token : accessToken} ,function(res) {
                  for(var i = 0 ; i < res.data.length ; i++){
                        events.push(res.data[i].name);
                        $('#facebookGroups').append('<input type="radio" id="' + res.data[i].name + '"/>' + '<label for="' + res.data[i].name + '">' + res.data[i].name + '</label>').trigger('create');
                  }
            });
            FB.api('/me', {access_token : accessToken} ,function(respuesta) {
                  gender = respuesta.gender;
            });
            console.log(events);
            console.log(gender);
            $.ajax({
                type: 'GET',
                url:"http://127.0.0.1:3000/inituser",
                data: {
                    userGender : gender,
                    _id        : userID,
                    userEvents : events,
                    latitude   : userLatitude, 
                    longitude  : userLongitude,
                },
                dataType: 'jsonp',
                contentType: 'application/json',
                crossDomain: true,
                success: function(data){
                    console.log('data successfully sent');
                },
                error: function(){
                    console.log('there was an error');
                }
            });
            $.mobile.pageContainer.pagecontainer('change' , '#homepage');
            console.log(response);
        } else {
            $.mobile.pageContainer.pagecontainer('change' , '#login');
        }
    },{ scope: "email , user_groups , user_events" }); 
});

I cannot figure out how to access the gender and events variable. 我无法弄清楚如何访问性别和事件变量。 I need to send them to a remote server in the ajax call, but I cannot access them outside of the FB.api call. 我需要通过ajax调用将它们发送到远程服务器,但是我无法在FB.api调用之外访问它们。 I tried declaring them in the global scope, right after the loginButton function, and as as you can see inside the FB.login function, but nothing is working. 我试着宣布他们在全球范围内,在之后loginButton功能,并且你可以在里面看到FB.login功能,但没有什么工作。

It's obviously a scoping issue. 这显然是一个范围界定的问题。 Does anybody out there know if there is a solution? 外面有人知道是否有解决方案吗?

First of all, instead of the two calls- /me/groups and /me (for gender) - you can use just a single API - /me?fields=gender,groups 首先,您可以仅使用一个API来代替/me/groups/me (针对性别)这两个调用- /me?fields=gender,groups

The FB.api calls are asynchronous . FB.api调用是异步的 So you have to proceed after the success callback is received. 因此,您必须在收到成功回调后继续进行。 To do that, you can follow this structure- 为此,您可以按照以下结构进行操作-

loginButton.on('click', function(e) {
   e.preventDefault();
   FB.login(function(response) {
      var gender;
      var events = [];
      if (response.status === 'connected') {
         userID = response.authResponse.userID;
         var accessToken = response.authResponse.accessToken;

         GetUserDetails();   // 

      } else {
        $.mobile.pageContainer.pagecontainer('change' , '#login');
      }
  },{ scope: "email , user_groups , user_events" }); 
});

function GetDetails(){
   FB.api('/me?fields=gender,groups' ,function(res) {
       userId = res.id;
       gender = res.gender;
       for(var i = 0 ; i < res.groups.data.length ; i++){
          events.push(res.groups.data[i].name);
          $('#facebookGroups').append('<input type="radio" id="' + res.groups.data[i].name + '"/>' + '<label for="' + res.groups.data[i].name + '">' + res.data[i].name + '</label>').trigger('create');
       }
       console.log(events);
       console.log(gender);
       SendToAjax(userID, gender, events);
   });
}   

function SendToAjax(userID, gender, events){ 
    ....
     // your ajax call here
    ....
}

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

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