I would say I'm a beginner transitioning into intermediate programmer and I don't completely understand closures as well as nested functions with return values. I've done some reading and can't really wrap my head around it.
The following code will only return undefined. I'm going to assume this is because it's a nested function but I'm not sure I understand how to fix it.
var Twit = require('twit');
var request = require('request');
var T = new Twit({
consumer_key: 'Removed for security reasons',
consumer_secret: 'Removed for security reasons',
access_token: 'Removed for security reasons',
access_token_secret: 'Removed for security reasons',
});
var stream = T.stream('statuses/filter', {track: '#instagram'});
stream.on('tweet', function(tweet){
if(!tweet.entities.media){
console.log("No photo here");
}else{
var imageUrl = JSON.stringify(tweet.entities.media[0].media_url).replace(/^"(.*)"$/, '$1');
console.log(describeImage(imageUrl) + " - " + imageUrl);
}
});
function describeImage(imageUrl){
var options = {
url: "https://api.projectoxford.ai/vision/v1.0/describe?maxCandidates=1",
json: {url: imageUrl},
method: 'POST',
headers: {
'Content-type' : 'application/json',
'Ocp-Apim-Subscription-Key' : 'Removed for security reasons'
}
}
request(options, function(err, res, body){
if(err){
console.log(err);
}
//This is where I'm going wrong.
return JSON.stringify(body.description.captions[0].text);
});
}
Any help would be wonderful!