0

what I am trying to do here is to get the latest posts from instagram and than store the Id of the users who liked my post. I am using instafeed.js for my development. I have tried reading a lot of post for "how to grab a value from an ajax call" like : how to grab return value from an ajax call? , How do I return the response from an asynchronous call? But still haven't figured how to use a callback on my code. An answer would be much appreciated. Thanks!

var userFeed = new Instafeed({
    get: 'user',
    userId: 'xxx',
    clientId: 'xxxx',
    accessToken: 'xxxx',
    resolution: 'thumbnail',
    template: '<a href="{{link}}" target="_blank" id="{{id}}"><div class="insta_story"><img class="insta_foto" src="{{image}}" /><span class="likes_and_comment">{{likes}} - {{comments}}</span></a></div>',
    sortBy: 'most-recent',
    limit: 20,
    links: false,
    success : function mediaData(data){
        console.log(data);
    }
});
userFeed.run();

What I would like now is to get the data object that contains my information.

Community
  • 1
  • 1
  • 1
    The `data` is only accessible in the `success` callback function. Put all your logic in there – Rory McCrossan May 16 '17 at 08:45
  • try to debug the success callback with the console in the browser. The response of the server should be the data variable – Sepultura May 16 '17 at 08:47
  • `console.log(data);` - you can see it right there, in the console. `console.log(JSON.stringify(data));` might make it easier to read. Unless your call throws an error of course, in which case you ought to handle the "error" callback as per the instafeed docs – ADyson May 16 '17 at 08:55
  • @ADyson yes I already know that I can see my object, but what i want is to get that value outside the ajax call because I want to store it in the database through php. But when I try to access the object outside the ajax call of course that's undefined. Some practical help would be much appreciated. –  May 16 '17 at 09:19
  • Anything you want to do with the values you get from the ajax call, must be done within (or at least called from) the "success" callback. As you point out, `data` will be undefined outside there because of variable scope, just like any function (a callback is just a function, nothing scary). So if you wanted to send the value somewhere else (e.g. to a PHP endpoint via another ajax call), make a function like `function sendToServer(data) { .. }`. Then in the "success" callback, simply write `sendToServer(data);` which will trigger that function. Just like you would trigger any other code :-) – ADyson May 16 '17 at 09:25
  • 1
    @ADyson I get it now. Just solved it. Really really I am so thankful, you helped me a lot in my growth as a developer. Thanks a lot :) ! –  May 16 '17 at 09:33

0 Answers0