Node.JS:Watson语言翻译 - 无法将翻译结果存储在变量

时间:2016-01-24 22:20:44

标签: javascript node.js ibm-watson

watson-developer-cloud中所述,我使用以下代码翻译某些文字:

var watson = require('watson-developer-cloud');

var language_translation = watson.language_translation({
  username: '<username>',
  password: '<password>',
  version: 'v2'
});

var myTranslation = {};//is supposed to hold the translation provided by the following code:

language_translation.translate({
text: 'A sentence must have a verb', source : 'en', target: 'es' },
function (err, translation) {
  if (err)
    console.log('error:', err);
  else{
    myTranslation = JSON.stringify(translation, null, 2);
    console.log(myTranslation);//print out the JSON with translation data
  }
});
Here Begin problems:
console.log(myTranslation); // Print out an empty hash...

为什么myTranslationelse语句中保留了正确的数据,但在它之外,它是否包含空哈希?

0 个答案:

没有答案