I have the following JavaScript code which gets all of the id’s of the albums of the user who’s currently logged in:
function fqlQuery(){
showLoader(true);
var albums= new Array();
FB.api('/me', function(response) {
var query = FB.Data.query('SELECT aid, owner, name, object_id FROM album WHERE owner={0}', response.id);
var aantal_albums=0;
var photos= new Array();
query.wait(function(rows) {
aantal_albums= rows.length;
for(var i=0; i<aantal_albums;i++){
albums[i]= rows[i].aid;
}
});
});
alert(albums[1]);
}
My issue is, the last line, the alert is not working. It is called outside of the function in which the array is filled, but that should be fine, because the array is declared at the top (var albums= new Array), right?
I feel like there’s an elephant in the room and I can’t see it. Do you see anything wrong? I don’t know if this is a fql problem or JavaScript.
You’re not thinking asyncronously; the array is not going to be populated until the callback you passed to
FB.apiruns, and youralertexecutes before that.Place the
alertinside of your callback function and you should get the results you expect.