未捕获的TypeError:无法读取未定义的属性'length',javascript

时间:2013-06-26 11:13:00

标签: javascript variables undefined

我正在开发一个jquery移动应用程序,它使用Facebooks Javascript API从Facebook页面导入albumsphotos

我的javascript代码尝试执行此操作:

1)通过API调用获取albums,我保留album namecover_photo_link id和{{每个number of the photos的1}}。

2)通过API调用获取每个album的{​​{1}},我将每个photosalbum保持为 二维数组名为photo link和 分别为thumbnail link

3)动态尝试创建albumPhotos[x][y] thas,每张专辑都有albumThumnails[x][y],当用户选择一张导航到照片时。

我的javascript代码,如下所示:

listView

我的问题在于:

name/thumbnail

我收到错误:未捕获的TypeError:无法读取未定义的属性'length',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>'); for(var k=0; k<response.data.length; k++){ albumThumbnails[i][k] = response.data[k].picture; albumPhotos[i][k] = response.data[k].source;} ,我通过将其重写为此来修复它:

picture

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

0 个答案:

没有答案