[英]module.exports returning undefined
I`m trying to make a translation function with IBM Watson API in " services/ibmWatson/index.js ".我正在尝试在“ services/ibmWatson/index.js ”中使用 IBM Watson API 制作翻译功能。 I receive the response correctly, but when I return the response to the " IncidentController.js " it receives as undefined.
我正确地收到了响应,但是当我将响应返回给“ IncidentController.js ”时,它收到的未定义。
const LanguageTranslatorV3 = require('ibm-watson/language-translator/v3');
const { IamAuthenticator } = require('ibm-watson/auth');
module.exports = {
async translate(phrase, language) {
const languageTranslator = new LanguageTranslatorV3({
authenticator: new IamAuthenticator({ apikey: '<my_API_key>' }),
url: 'https://gateway.watsonplatform.net/language-translator/api/',
version: '2020-03-28',
});
await languageTranslator.translate(
{
text: phrase,
source: 'pt',
target: language
})
.then(response => {
if(response.status=200){
console.log(response.result.translations);
return(response.result.translations);
}
return (["error"]);
})
.catch(err => {
console.log('error: ', err);
return (["error"]);
});
}
}
In the above code the console.log(response.result.translations) returns correctly:在上面的代码中, console.log(response.result.translations)正确返回:
[ { translation: 'Dog run over.' },
{ translation: 'Castration' },
{ translation: 'Ticks' },
{ translation: 'Tuberculosis' } ]
In the in IncidentController.js:在 IncidentController.js 中:
const Watson = require('../../src/services/ibmWatson');
const db_connection = require('../database/connection');
module.exports = {
async index(request, response) {
const incidents = await db_connection('incidents').join('ongs', 'ongs.id', '=', 'incidents.ong_id')
.select([
'incidents.*',
'ongs.name',
'ongs.uf']
);
const titles = [];
incidents.forEach((incident) => { titles.push(incident.title) });
const translated_titles = await Watson.translate(titles, "en");
console.log(translated_titles);
return response.json(incidents);
}
}
In the above code the console.log(response.result.translations) returns undefined .在上面的代码中, console.log(response.result.translations)返回undefined 。
What is wrong with it?它有什么问题?
You are returning response.result.translations
to the response
callback from then()
.您将从
then()
返回response.result.translations
到response
回调。 Since that callback cannot be accessed by your IncidentController, it returns undefined
.由于您的 IncidentController 无法访问该回调,因此它返回
undefined
。 This is one way to solve this problem:这是解决此问题的一种方法:
// services/ibmWatson/index.js
translate(phrase, language) {
return new Promise((resolve, reject) => {
try {
const response = await languageTranslator.translate({ /* options */ });
resolve(response); // Return the translations
} catch(error) {
reject(error); // If there's an error, return the error
}
});
}
// IncidentController.js
async index() {
// ...
const translatedTitles = await Watson.translate(titles, "en");
console.log(translatedTitles); // Should be working now
}
I hope I could help you or at least lead you in the right direction.我希望我能帮助你或至少引导你走向正确的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.