问题
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