TiernO
TiernO

Reputation: 447

AWS.translate is not a constructor

I am writing the following lambda function in the aws website, trying to get the basics of aws translate working but i get the error AWS.translate is not a constructor

I have looked it up and it means translate is not in the aws-sdk version I have imported, but how can i make it so that is is?

console.log('Loading function');
var AWS = require('aws-sdk');
var translate = new AWS.translate();

exports.handler = async (event, context) => {
    var params = {
          SourceLanguageCode: 'en', /* required */
          TargetLanguageCode: 'es', /* required */
          Text: 'Hello World', /* required */
    };
    translate.translateText(params, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else     console.log(data);           // successful response
    });
};

Upvotes: 0

Views: 482

Answers (2)

jogold
jogold

Reputation: 7407

It should be AWS.Translate() not AWS.translate().

Also, if working with async, prefer the try/catch version using .promise():

console.log('Loading function');
const AWS = require('aws-sdk');
const translate = new AWS.Translate({ apiVersion: '2017-07-01' }); // Fix API version (best practice)

exports.handler = async (event, context) => {
  try {
    const params = {
      SourceLanguageCode: 'en', /* required */
      TargetLanguageCode: 'es', /* required */
      Text: 'Hello World', /* required */
    };
    const data = await translate.translateText(params).promise();
    console.log(data);
  } catch (err) {
    console.log(err, err.stack);
  }
};

Upvotes: 1

Mark B
Mark B

Reputation: 200527

Translate should be a capital T like this:

var translate = new AWS.Translate();

As documented here.

Upvotes: 0

Related Questions