2010-07-27 67 views
1

這對我來說很怪異。所以,如果我不只有在代碼被轟出之前我纔會警告代碼纔有效?

function BindAlbumAndPhotoData() 
    { 
     // Get an array of all the user's Albums 
     var aAlbums = GetAllAlbums(userID, token); 

     alert("aAlbums: " + aAlbums); 
     if (aAlbums == null || aAlbums == "undefined") 
      return; 

     // Set the default albumID 
     var defaultAlbumID = aAlbums[0].id; 

    }; 

所以我得到一個未定義的錯誤行var defaultAlbumID = aAlbums [0] .id;如果我不取消註釋警報(「aAlbums:」+ aAlbums);

這是什麼?如果我註釋掉警報(「aAlbums:」+ aAlbums);那麼我得到一個未定義的var defaultAlbumID = aAlbums [0] .id;

這太奇怪了。我一整晚都在努力弄清楚爲什麼我一直在爲aAlbum [0]定義一個未定義的數據,並且一旦我添加了一個我以前已經使用過的警報,一切都很好......對我來說毫無意義。

這裏的GetAllAlbums的全碼:

​​

所以我不返回數組,直到我打了FB.api異步調用的回調,所以我看不出我的defaultAlbumID = aAlbums [ 0] .ID;在我有一個有效的數據數組之前,正在執行一行代碼。當我加入警報時,它會延遲到達我的行defaultAlbumID = aAlbums [0] .id;導致它我想幸運地有數據beacuse異步FB.api調用已完成,但我再也看不到如果有這樣的問題,當我等待調用之前繼續和返回數組aAlbums在我的BindAlbumAndPhotoData()方法中。

更新#3

​​

這不是打我的回調警報。我仍然在這裏做錯了事。

UPDATE#4 - 由於某種原因,它不會觸及我的FB.api回調。

function GetAllAlbums(userID, accessToken, callbackFunctionSuccess) 
{ 
    var aAlbums = []; // array 
    var uri = "/" + userID + "/albums?access_token=" + accessToken; 

    alert("uri: " + uri); 

    FB.api(uri, function (response) 
    { 
     // check for a valid response 
     if (!response || response.error) 
     { 
      alert("error occured"); 
      return; 
     } 

     for (var i = 0, l = response.data.length; i < l; i++) { 
      alert("Album #: " + i + "\r\n" + 
        "response.data[i].id: " + response.data[i].id + "\r\n" + 
        "response.data[i].name: " + response.data[i].name + "\r\n" + 
        "response.data[i].count: " + response.data[i].count + "\r\n" + 
        "response.data[i].link: " + response.data[i].link 
       ); 

      aAlbums[i] = new Album(
                response.data[i].id, 
                response.data[i].name, 
                response.data[i].count, 
                response.data[i].link 
                ); 

      alert("aAlbums[" + i + "].id : " + aAlbums[i].id); 
     } 

     alert("about to pass back the array to the callback function"); 
     // pass the array back to the callback function sent as a param to the GetAllAlbums method here 
     callbackFunctionSuccess(aAlbums); 
    }); 
} 
+0

你可能在這裏做ajax('GetAllAlbums(userID,token);'),你不是嗎? – Reigel 2010-07-27 04:21:56

+2

GetAllAlbums(userID,token);'做什麼? – Hristo 2010-07-27 04:22:55

+0

是的......但我仍然不明白。 – PositiveGuy 2010-07-27 04:23:13

回答

4
function BindAlbumAndPhotoData() 
{ 
    // Get an array of all the user's Albums 
    GetAllAlbums(userID, token, function(aAlbums){ 

     // Set the default albumID 
     var defaultAlbumID = aAlbums[0].id; 

    }); 

}; 

然後在GetAllAlbums函數調用成功功能,當你有數據備份

// *********休息後******* //

爲了迴應更新的問題:FB API大部分是異步的,並且會在等待時繼續執行其他代碼。因此,使用你的代碼,所有我做的函數傳遞,然後調用函數你在最後

function GetAllAlbums(userID, accessToken, funcSuccess) 
{ 
    var aAlbums = []; // array 
    var uri = "/" + userID + "/albums?access_token=" + accessToken; 

alert("uri: " + uri); 

FB.api(uri, function (response) 
{ 
    // check for a valid response 
    if (!response || response.error) 
    { 
     alert("error occured"); 
     return; 
    } 

    for (var i = 0, l = response.data.length; i < l; i++) 
    { 
     alert("Album #: " + i + "\r\n" + 
       "response.data[i].id: " + response.data[i].id + "\r\n" + 
       "response.data[i].name: " + response.data[i].name + "\r\n" + 
       "response.data[i].count: " + response.data[i].count + "\r\n" + 
       "response.data[i].link: " + response.data[i].link 
      ); 

     aAlbums[i] = new Album(
               response.data[i].id, 
               response.data[i].name, 
               response.data[i].count, 
               response.data[i].link 
               ); 

     alert("aAlbums[" + i + "].id : " + aAlbums[i].id); 



    } 

    funcSuccess(aAlbums); 
}); 

}

+0

我正在使用Facebook SDK製作JS ajax調用。 – PositiveGuy 2010-07-27 04:31:04

+0

然後,而不是在那裏的功能jquery位,從我看過你的FB例子,你會想要的東西像ajax.ondone = function(data){funcSuccess(data); }; 我把它包裹在另一個函數中的唯一原因是它讓你有空間做一些質量控制(如果數據!= null)等 – 2010-07-27 04:34:54

+0

傑米,謝謝。看看我的更新以上的線程...看到我的功能有GetAllAlbums。 – PositiveGuy 2010-07-27 04:44:15

0

嘗試修改您的情況是這樣的:

if (typeof aAlbums == 'undefined') 
     return; 

還要確保aAlbums具有價值,是一個數組:

alert(aAlbums.length); 

或者:

for(var i = 0; i < aAlbums.length; i++) 
{ 
    alert(aAlbums[i].id); 
} 
+0

嗯,即使我知道該數組的值有奇怪,我也會得到零長度。 – PositiveGuy 2010-07-27 04:30:06

+0

if(typeof aAlbums =='undefined') return;不起作用或有任何區別..只是測試。 – PositiveGuy 2010-07-27 04:32:34

+0

@coffeeaddict:如果它的長度是0,那麼它沒有填充數據,不是數組。確保函數'GetAllAlbums'正常工作。 – Sarfraz 2010-07-27 04:34:33

1

通過它嘗試三等於跡象,而不是兩個,也......返回虛假而非虛無。

if (aAlbums === null || aAlbums === undefined) 
      return false; 

此外,不確定不需要加引號,否則,它只是被認爲與「不確定」

值上的補充說明一個字符串,它可能會更好也檢查aAlbums實際上是一個數組,然後再決定從中返回一個密鑰。

if ( aAlbums === null 
    || aAlbums === undefined 
    || (typeof(aAlbums)=='object'&& !(aAlbums instanceof Array)) 
    } return false; 
+0

正如你所說的那樣,它是'undefined'而不是'undefined'' – 2010-07-27 04:37:38

+0

這並沒有什麼區別:if(aAlbums === null || aAlbums === undefined ||(typeof(aAlbums)==' object'&&!(aAlbums instanceof Array)) )return false; – PositiveGuy 2010-07-27 04:47:51

+0

請參閱我的主線程更新..添加GetAllAlbums函數,向您展示這是如何工作的。 – PositiveGuy 2010-07-27 04:55:29

1

您的函數GetAllAlbums()是否執行一些HTTP請求?如果是這樣,那麼你需要使這個調用同步,或者你需要把你的代碼放到一個函數中,並將其作爲回調傳遞給Ajax請求。

+0

是的,這已經在FB.api的回調中。看到我更新的帖子。 – PositiveGuy 2010-07-27 04:51:31