2012-03-04 73 views
0

我想在其他功能中使用用戶的Facebook標識。我用了回報,但沒有奏效。我的代碼有什麼問題?使用javascript sdk獲取Facebook用戶標識。並使用全球?

function get_id(){ 
    FB.login(function(response) { 
      if (response.authResponse) { 
       var token = response.authResponse.accessToken; 
       FB.api('/me?access_token='+token+'', function(response) { 
        var id = response.id; 
        return id; 
       }); 
      } 
    }); 
    } 
    var user_id = get_id(); 

這是一個Facebook應用程序。而且,我用多個朋友選擇器來代碼,但我想使用用戶ID全局。

回答

2

您不能從異步函數返回結果。讓你的函數接受一個回調,並繼續在那裏。

function get_id(cb) { 
    FB.login(function (response) { 
     if (response.authResponse) { 
      var token = response.authResponse.accessToken; 
      FB.api('/me?access_token=' + token + '', function (response) { 
       cb(response.id); 
      }); 
     } 
    }); 
} 

get_id(function (user_id) { 
    console.log(user_id); 
}); 
+2

我知道你只是複製粘貼自己的代碼作爲你的答案的基礎,但手動添加標記是沒有必要的(並且是沒有意義的) – zerkms 2012-03-04 23:14:14

+0

謝謝!我現在明白了。 :) – 2012-03-04 23:18:43

+0

和我怎樣才能得到返回形式的get_id函數??我需要另一個功能的用戶ID。 – 2012-03-04 23:27:05

相關問題