JQBolo JQBolo - 1 month ago 10
Javascript Question

Dealing with facebook Graph API objects in javascript

I am trying to access specific albums via their location from a Facebook page via a graph API call and ultimately display them on a webpage.

Essentially I think I need to loop through the object that Facebook sends back (response in this case) and only store objects which contain the substring "Germany" in the location description to the variable albums. Currently nothing happens in the console when I trigger the method.

I'm kind of stuck at this point, I can make API calls and have them returned from the specific page via other functions, so my permission seems to be fine, I'm just structuring this function incorrectly (I think, I'm relatively new to JS and Jquery).

model.getPhoto = (function(data){
var albums;
FB.api(
'/--USER ID GOES HERE--/albums',
'GET',
{"fields":"link,location"},
function(response){
for (var i=0; i<response.length; i++){
if (response.albums.location[i] = "Germany"){
albums = response.albums.location[i];
i++;
}
console.log(albums);
}
}
);
});

Answer

You are setting model.getPhoto as a new function. If your code is complete, you never call this function. I added some lines then you can try to fix and see some results... Also changed some lines, created albums as an Array and put i++ outside of if to execute for loop correctly. Last line calls your function.

model.getPhoto = (function(data){
    FB.api('/--USER ID GOES HERE--/albums',
           'GET',     
           {"fields":"link,location"},
           function(response){
               var albums = new Array();
               for (var i=0; i<response.length; i++){
                   if (response.albums.location[i] == "Germany"){
                       albums.push(response.albums.location[i]);
                   }
                   i++;
                   console.log(albums);
               }
          });
     });
model.getPhoto({var1: "value1"});

I suggest you play more with javascript and logics, take a look inside some libraries like jQuery itself, as you are not using it here, just pure JS, and have fun!

EDIT

When comparing, you must use == instead of =

Best, Eder