Reputation: 3725
I am doing two calls off node to facebook graph, one to get the list of album ids and another to get the cover photo url for that id and add that into the original json object.
How do I structure it to do something with the data object after the loop to get and add the cover photo urls? current code only contains the data before the loop
$('#main').on('click', '#albums', function() {
$.get('/albums', function(data) {
var a = 0;
for (i = 0; i < data.data.length; i++) {
$.get('/cover-photo?id=' + data.data[i].cover_photo, function(dataz) {
data.data[a].cover_photo_url = dataz.location;
a++;
});
}
});
// do something with the data obj
});
Upvotes: 0
Views: 75
Reputation: 156
As I understand your problem is that you just have access to the data in the loop. The trick to resolve that is simple. You just have to assign a global var. Do it like this:
var global_data="";
$('#main').on('click', '#albums', function() {
$.get('/albums', function(data) {
var a = 0;
for (i = 0; i < data.data.length; i++) {
$.get('/cover-photo?id=' + data.data[i].cover_photo, function(dataz) {
global_data = data;
data.data[a].cover_photo_url = dataz.location;
a++;
});
}
});
// now you could access global_data here
});
Upvotes: 1