Facebook 使用自定义函数从FB.api调用返回查询结果

Facebook 使用自定义函数从FB.api调用返回查询结果,facebook,facebook-graph-api,request,facebook-javascript-sdk,Facebook,Facebook Graph Api,Request,Facebook Javascript Sdk,我有以下代码: getThirdPartyID : function () { return FB.api("/me?fields=third_party_id", function (userData) { console.debug("Your Facebook ThirdPartyId is: " + userData["third_party_id"]); return userData["third_p

我有以下代码:

getThirdPartyID : function () {                     
    return FB.api("/me?fields=third_party_id", function (userData) { 
        console.debug("Your Facebook ThirdPartyId is: " + userData["third_party_id"]);
        return userData["third_party_id"];
    });
},

但它返回为空。这个代码有什么问题?我怎样才能用同样的想法访问它?tnx是一个函数,它对facebookapi进行异步请求,但不返回任何内容。您只能在
回调
中获得结果。您应该利用不同的方法来实现这一点:

var someObj = {
  getThirdPartyID : function (thirdPartyIDCallback) {
    return FB.api("/me?fields=third_party_id", function (userData) { 
      console.debug("Your Facebook ThirdPartyId is: " + userData["third_party_id"]);
      thirdPartyIDCallback(userData["third_party_id"]);
    });
  }
}

var handleThirdPartyID = function(thirdPartyID){
  // do something with thirdPartyID
  alert(thirdPartyID);
}
someObj.getThirdPartyID(handleThirdPartyID);

FB.api
是一个函数,它对Facebook api执行异步请求,但不返回任何内容。您只能在
回调
中获得结果。您应该利用不同的方法来实现这一点:

var someObj = {
  getThirdPartyID : function (thirdPartyIDCallback) {
    return FB.api("/me?fields=third_party_id", function (userData) { 
      console.debug("Your Facebook ThirdPartyId is: " + userData["third_party_id"]);
      thirdPartyIDCallback(userData["third_party_id"]);
    });
  }
}

var handleThirdPartyID = function(thirdPartyID){
  // do something with thirdPartyID
  alert(thirdPartyID);
}
someObj.getThirdPartyID(handleThirdPartyID);

api工作异步。这意味着您的函数在FB.api回调函数返回之前返回

您应该将FB.api的返回值设置为一个变量,或者在FB.api回调函数中调用其他函数

function GetUserData(val){
 alert(val);
}
getThirdPartyID : function () {                     
    FB.api("/me?fields=third_party_id", function (userData) { 
        console.debug("Your Facebook ThirdPartyId is: " + userData["third_party_id"]);
        GetUserData(userData["third_party_id"]);
    });


};

api工作异步。这意味着您的函数在FB.api回调函数返回之前返回

您应该将FB.api的返回值设置为一个变量,或者在FB.api回调函数中调用其他函数

function GetUserData(val){
 alert(val);
}
getThirdPartyID : function () {                     
    FB.api("/me?fields=third_party_id", function (userData) { 
        console.debug("Your Facebook ThirdPartyId is: " + userData["third_party_id"]);
        GetUserData(userData["third_party_id"]);
    });


};

谢谢,也许这个答案和第二个一样。它帮助了我!谢谢,也许这个答案和第二个一样。它帮助了我!