watson-developer-cloud / node-sdk

:comet: Node.js library to access IBM Watson services.
https://www.npmjs.com/package/ibm-watson
Apache License 2.0
1.48k stars 669 forks source link

[visual-recognition] defaultUrl is undefined #276

Closed Startouf closed 8 years ago

Startouf commented 8 years ago

I am calling Watson library with deasync. Contents of my file lib/watson_image_recognition

const Watson = require('watson-developer-cloud');
const deasync = require('deasync');
const VisualRecognition = Watson.visual_recognition({
  url: 'https://gateway-a.watsonplatform.net/visual-recognition/api',
  api_key: 'deb4cf71cff78dee4770f2bc6f65aa0e06d7ceda',
  version: 'v3',
  version_date: '2016-05-19'
});
const deasyncedVisualRecognition = deasync(VisualRecognition.classify);

module.exports = {
  recognize: function(imagePath) {
    var params = {
      images_file: fs.createReadStream(imagePath)
    };
    try {
      const res = deasyncedVisualRecognition(params);
      var results = [];
      for(var i=0;i<res.images[0].labels.length;i++) {
        results.push(res.images[0].labels[i].label_name);
      }
      console.log('got '+results.length+' labels from good ole watson');
      return results;
    } catch(error) {
      console.log(error);
      return null;
    }
  }
}

Should get results of classify function

I get an error

 TypeError: Cannot read property 'url' of undefined
<     at createRequest (C:\Dev\myProject\nodejs\node_modules\watson-developer-cloud\lib\requestwrapper.js:130:46)
<     at VisualRecognitionV3.classify (C:\Dev\myProject\nodejs\node_modules\watson-developer-cloud\services\visual_recognition\v3.js:205:10)
<     at C:\Dev\myProject\nodejs\node_modules\deasync\index.js:43:7
<     at Object.module.exports.recognize (C:\Dev\myProject\nodejs\lib\watson_image_recognition.js:38:19)

After looking at your code, it would seem those lines from requestwrapper are causing the crash

// Add service default endpoint if options.url start with /
  if(options.url.charAt(0) === '/'){
      options.url = parameters.defaultOptions.url + options.url; //parameters.defaultOptions is undefined
  }

It would seem that the line this._options = extend(serviceDefaults, omit(options, ['version_date'])); does not do its job as intended in v3.js ?

If I replace parameters.defaultOptions.url by 'https://gateway-a.watsonplatform.net/visual-recognition/api` it works again

nfriedly commented 8 years ago

It looks like you're just loosing the scope when you desync the classify function. Try changing

const deasyncedVisualRecognition = deasync(VisualRecognition.classify);

to

const deasyncedVisualRecognition = deasync(VisualRecognition.classify.bind(VisualRecognition));