I'm trying to return a string of an Instagram API url. Not an object, not the data, but when getUrl()
is called, it returns a string which I can then use in a getJSON later on.
The tricky bit is, thanks to the Instagram API, I first need to generate the user ID from the username (that's the getJSON you see, which works fine by the way), store that ID as user_id, then insert that ID into my string.
I understand what I have at the moment isn't working because ajax is async, and also because user_id is being used out of scope. So my question is.. what do I need to do? I feel like I'm close.
var username = foo;
var client_id = 1234567890;
function getUrl() {
function getUserID() {
return 'https://api.instagram.com/v1/users/search?q=' + username + '&callback=?&client_id=' + client_id;
}
$.getJSON(getUserID(), function (data) {
var user_id = data.data[0].id;
});
return 'https://api.instagram.com/v1/users/' + user_id + '/media/recent/?&client_id=' + client_id;
}
console.log(getUrl);