TypeError: Translate is not a constructor

时光毁灭记忆、已成空白 提交于 2019-12-11 14:33:17

问题


This is the code for transalting english to ru

'use strict';
const {Translate} = require('@google-cloud/translate');
const projectId = 'godwin-feb8f';
const translate = new Translate({
 projectId: projectId,
});
const text = 'Hello, world!';
const target = 'ru';
translate
.translate(text, target)
.then(results => {
const translation = results[0];
console.log(`Text: ${text}`);
console.log(`Translation: ${translation}`);
 })
.catch(err => {
console.error('ERROR:', err);
}); 

TypeError: Translate is not a constructor error occurs and if we declare it as function the error clears

but the next error appears for const declaration


回答1:


const {Translate} = require('@google-cloud/translate');

results in Translate being undefined.

As the documentation states, Translate is @google-cloud/translate module exports:

const Translate = require('@google-cloud/translate');



回答2:


var Translate = require('@google-cloud/translate');

var translate = new Translate.Translate({ projectId: projectId, });



来源:https://stackoverflow.com/questions/52020086/typeerror-translate-is-not-a-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!