2012-03-21 56 views
0

我从facebook获取专辑名称并使用下面的代码将它们写在页面上。 问题是它还包括未定义的,所以它写了两个“墙照片”,所以我怎样才能防止它写出未定义的?来自facebook api的未定义响应数据?

FB.api('/xxxxxxxxxxxxxxx/albums', function(response) { 

    if (response && response.data && response.data.length){ 
    var ul = document.getElementById('fb-albumslist'); 
    for (var i=0, l=response.data.length; i<l; i++) { 
    if (response.data !== "undefined"){  
     var 
     album = response.data[i], 
     li = document.createElement('li'), 
     a = document.createElement('a'); 
     a.innerHTML = album.name; 
     a.href = album.link; 
     li.appendChild(a); 
     ul.appendChild(li); 
    } 
    } 
    } 
    }); 

在页面上它写出:

Wall Photos 
Wall Photos (this is undefined) 
Cover Photos 
Profile Picture 

林与尝试“{但是我不知道怎么做‘(未定义如果response.data ==!’)” ? 任何输入赞赏,谢谢!

回答

1

看来,Facebook正在创建具有这些特性的另一面墙上相簿:

{ 
    "id": "YYYY", 
    "from": { 
    "name": "AAAA", 
    "id": "XXX" 
    }, 
    "name": "Wall Photos", 
    "privacy": "custom", 
    "type": "friends_walls", 
    "created_time": "2011-02-02T08:54:20+0000", 
    "updated_time": "2011-02-02T08:54:20+0000", 
    "can_upload": false 
}, 

我不知道这张专辑的目的而做的最好的事情就是检查link场否则存在跳过:

if(!album.link) continue; 
+0

喜ifaour和感谢,但我不能得到它的工作,如果我用你的代码,我得到这个错误:类型错误:“未定义”不是(评估“album.link”)和Im对象不知道该怎么办?我把它放在“if(response.data!==”undefined“){” – 2012-03-22 08:16:34

+0

只是放在这行之前:'a.innerHTML = album.name;' – ifaour 2012-03-22 09:58:18

+1

非常感谢ifaour,效果很棒! – 2012-03-24 07:00:10