0

我正在开发一个 jquery 移动应用程序,用于从 facebook 页面Facebooks Javascript API导入albums和。photos

我的 javascript 代码尝试这样做:

1)albums通过API调用获取我保存的每个album namecover_photo_linktheid和the 。number of the photosalbum

2)使用 API 调用获取photosevery album,在该 API 调用中,我将 everyphoto linkthumbnail linkto二维数组分别称为albumPhotos[x][y]albumThumnails[x][y]

3)动态地尝试创建一个listView拥有name/thumbnail每个相册的相册,当用户选择一个时,导航到照片。

我的 javascript 代码,如下所示:

<script>
        var albumPhotos = new Array();
        var albumThumbnails = new Array();
        window.fbAsyncInit = function() {
            // init the FB JS SDK 
            FB.init({
                appId      : '564984346887426',                    // App ID from the app dashboard
                channelUrl : 'channel.html',                       // Channel file for x-domain comms
                status     : true,                                 // Check Facebook Login status
                xfbml      : true                                  // Look for social plugins on the page
            });

            // Additional initialization code such as adding Event Listeners goes here
            FB.api('169070991963/albums', function(response) {
                if(!response || response.error) {
                    // render error
                    alert("Noo!!");
                } else {
                    // render photos
                    for(var i=0; i<response.data.length; i++){  
                        (function (i) {
                            var albumName = response.data[i].name;
                            var albumCover = response.data[i].cover_photo;
                            var albumId = response.data[i].id;
                            var numberOfPhotos = response.data[i].count;

                            FB.api(albumId + "/photos", function(response) {
                                if(!response || response.error) {
                                    // render error
                                    alert("Noo!!");
                                } else {
                                    for(var k=0; k<response.data.length; k++){ 
                                         albumThumbnails[i] =  albumThumbnails[i]||{};
                                         albumThumbnails[i][k] = response.data[k].picture;
                                         albumPhotos[i] = albumPhotos[i] || {};
                                         albumPhotos[i][k] = response.data[k].source;
                                    }
                                }
                            }); 

                            console.log(albumName);
                            FB.api( albumCover, function(response) {
                                if(!response || response.error) {
                                    // render error
                                    alert("Noo!!");
                                } else {
                                    // render photos
                                    $(".albums").append('<li>'+
                                        '<a href="#Gallery' + i + '"' + 'data-transition="slidedown">'+
                                        '<img src= "' + response.picture + '"  />'+
                                         '<h2>' + albumName + '</h2>'+
                                         '<p>' + "Number of Photos:  " + numberOfPhotos +'</p>'+
                                         '</a>'+
                                         '</li>')
                                    .listview('refresh');

                                    $("#home").after('<div data-role="page" data-add-back-btn="true" id=Gallery'+ i +
                                        ' class="gallery-page"> ' +
                                        ' <div data-role="header"><h1>Gallery</h1></div> ' + ' <div data-role="content"> ' +
                                        ' <ul class="gallery"></ul> ' + ' </div> ' +
                                        ' </div> ');


                                    for(var x=0; x < albumPhotos[i].length; x++)
                                        $('#Gallery' + i + ' .gallery').append('<li><a href="' + albumPhotos[i][x] + '" rel="external"><img src="' +  albumThumbnails[i][x] + '" /></a></li>');

                                }
                            });
                        })(i);                                      
                    } //end of for loop
                console.log(albumPhotos);
                console.log(albumThumbnails);
                }
            });

        };

我的问题在这里:

for(var x=0; x < albumPhotos[i].length; x++)
        $('#Gallery' + i + ' .gallery').append('<li><a href="' + albumPhotos[i][x] + '" rel="external"><img src="' +  albumThumbnails[i][x] + '" /></a></li>');

我在哪里得到错误:未捕获的类型错误:无法读取未定义的属性“长度”,javascript

给你一个提示,事实是我在这段代码中也遇到了同样的错误:

for(var k=0; k<response.data.length; k++){ 
     albumThumbnails[i][k] = response.data[k].picture;
     albumPhotos[i][k] = response.data[k].source;}

where 属性picturesource无法读取,我通过将其重写为此修复它:

for(var k=0; k<response.data.length; k++){ 
    albumThumbnails[i] =  albumThumbnails[i]||{};
    albumThumbnails[i][k] = response.data[k].picture;
    albumPhotos[i] = albumPhotos[i] || {};
    albumPhotos[i][k] = response.data[k].source;}

知道为什么我会收到此错误吗?

4

0 回答 0