这对我来说太奇怪了。所以,如果我不
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的完整代码:
function GetAllAlbums(userID, accessToken)
{
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);
}
});
return aAlbums;
}
所以我没有返回数组,直到我点击FB.api异步调用的回调,所以我看不到我的defaultAlbumID = aAlbums [0] .id;在我有一个有效的数据数组之前,代码行正在执行。当我输入警报时,它会在它到达我的行defaultAlbumID = aAlbums [0] .id之前延迟它。幸运的是,由于我已经完成了同步FB.api调用已经完成,但我再也看不出在我继续操作并将数组返回之前等待调用的问题时甚至可能出现这样的问题。我的BindAlbumAndPhotoData()方法中的aAlbums。
更新#3
function BindAlbumAndPhotoData()
{
GetAllAlbums(userID, accessToken, function (aAlbums)
{
alert("we're back and should have data");
if (aAlbums === null || aAlbums === undefined) {
alert("array is empty");
return false;
}
var defaultAlbumID = aAlbums[0].id;
// Set the default albumID
var defaultAlbumID = aAlbums[0].id;
// Bind the album dropdown
alert(" defaultAlbumID: " + defaultAlbumID);
});
};
function GetAllAlbums(userID, accessToken, callbackFunctionSuccess)
{
var aAlbums = []; // array
var uri = "/" + userID + "/albums?access_token=" + accessToken;
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);
}
// pass the array back to the callback function sent as a param to the GetAllAlbums method here
callbackFunctionSuccess(aAlbums);
});
}
它没有在回调中触及我的警报。我必须在这里做错事。
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 :(得分: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函数中,当您返回数据时调用success函数
// *********在休息之后******* //
响应更新的问题: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);
});
}
答案 1 :(得分:1)
尝试三个等号而不是两个,并且......返回假而不是一无所有。
if (aAlbums === null || aAlbums === undefined)
return false;
另外,undefined不需要引号,否则,它只被认为是一个值为“undefined”的字符串
另外,在决定从中返回密钥之前,最好还是检查aAlbums是否实际上是一个数组。
if ( aAlbums === null
|| aAlbums === undefined
|| (typeof(aAlbums)=='object'&& !(aAlbums instanceof Array))
} return false;
答案 2 :(得分:1)
你的函数GetAllAlbums()
正在做一些HTTP请求吗?如果是这样,那么你需要将该调用同步,或者需要将代码放入函数中并将其作为回调传递给Ajax请求。
答案 3 :(得分:0)
尝试修改这样的条件:
if (typeof aAlbums == 'undefined')
return;
还要确保aAlbums
具有值并且是一个数组:
alert(aAlbums.length);
或者:
for(var i = 0; i < aAlbums.length; i++)
{
alert(aAlbums[i].id);
}