0

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!

  • `request` is asynchronous making `describeImage` asynchronous but you are trying to use it synchronously. Look into Promises (Bluebird) or callbacks. – Wainage May 15 '16 at 16:55

1 Answers1

0

Made two changes in code 1) where describeImage call was made and 2) where u were going wrong

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');
    describeImage(imageUrl,function(imgUrl){
       console.log(imgUrl + " - " + imageUrl);
    });
  }
});


function describeImage(imageUrl,callBack){
  
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);
    }
    //Call Callback function here
    return callBack(JSON.stringify(body.description.captions[0].text));
  });
  
  
}