我试图在这里获得我朋友的关系,但我给了所有正确的权限,但它仍然显示为我,未定义。它不会提取朋友的关系状态,也不会提取生日..下面是我的代码:
function loadFriendsrel()
{
//get array of friends
FB.api('/me/friends?fields=name,first_name,gender,picture,relationship_status,birthday', function(response) {
console.log(response);
var divContainer=$('.facebook-friends');
var testdiv2 = document.getElementById("test2");
for(var i=0; i<response.data.length; i++){
if(response.data[i].gender == 'female'){
testdiv2.innerHTML += response.data[i].first_name + '<br/>' + response.data[i].relationship_status + '<br/>' + ' ' + '<img src="' + response.data[i].picture + '"/>' + '<br /> <br/>';
}
}
});
}发布于 2012-05-24 20:52:56
即使你获得了所有权限,你也无法获得通过隐私设置阻止他们的用户的relationship_status。
隐私设置比facebook api具有更高的优先级。
因此,在你的循环中,一些朋友可能已经阻止了他们的relationship_status,所以它产生了undefined并打破了你的循环。
将您的循环更改为如下所示,
for(var i=0; i<response.data.length; i++){
if(response.data[i].gender == 'female'){
var relStatus = 'Relationship status not provided';
// If relationship_status exists, only then take its value
if('relationship_status' in response.data[i]){
relStatus = response.data[i].relationship_status;
}
testdiv2.innerHTML += response.data[i].first_name + '<br/>' + relStatus + '<br/>' + ' ' + '<img src="' + response.data[i].picture + '"/>' + '<br /> <br/>';
}
}您也可以将类似的逻辑应用于其他字段。
https://stackoverflow.com/questions/10737295
复制相似问题