aws-sdk-js for translate client results an error "cannot read property 'byteLength' of undefined"

1.1k views Asked by At

I run the AWS SDK V3 Translate Client in Node.js application to translate a sentence. This is my code:

const { TranslateClient, CreateParallelDataCommand } = require("@aws-sdk/client-translate");

const client = new TranslateClient({ 
  region: "ap-southeast-1",
  credentials: {
    accessKeyId: process.env.AWS_ACCESS_KEY_ID, 
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
  }
});

const params = {
  Text: "StackOverflow is very good.",
  SourceLanguageCode: "en",
  TargetLanguageCode: "id"
};

const command = new CreateParallelDataCommand(params);

function translate() {
  return client.send(command)
    .then((data) => {
      console.log(data);
    })
    .catch((reason) => {
      console.log(reason);
    })
}

translate();

I have followed the instruction from the documentation.

But, the result is as follows.

TypeError: Cannot read property 'byteLength' of undefined
    at Object.fromArrayBuffer (\projectDir\node_modules\@aws-sdk\util-buffer-from\dist-cjs\index.js:6:60)
    at castSourceData (\projectDir\node_modules\@aws-sdk\hash-node\dist-cjs\index.js:29:31)
    at Hash.update (\projectDir\node_modules\@aws-sdk\hash-node\dist-cjs\index.js:12:26)
    at hmac (\projectDir\node_modules\@aws-sdk\signature-v4\dist-cjs\credentialDerivation.js:36:10)
    at Object.getSigningKey (\projectDir\node_modules\@aws-sdk\signature-v4\dist-cjs\credentialDerivation.js:11:29)
    at SignatureV4.getSigningKey (\projectDir\node_modules\@aws-sdk\signature-v4\dist-cjs\SignatureV4.js:139:39)
    at SignatureV4.signRequest (\projectDir\node_modules\@aws-sdk\signature-v4\dist-cjs\SignatureV4.js:98:73)
    at async \projectDir\node_modules\@aws-sdk\middleware-signing\dist-cjs\middleware.js:13:18
    at async StandardRetryStrategy.retry (\projectDir\node_modules\@aws-sdk\middleware-retry\dist-cjs\StandardRetryStrategy.js:51:46)
    at async \projectDir\node_modules\@aws-sdk\middleware-logger\dist-cjs\loggerMiddleware.js:6:22 {
  '$metadata': { attempts: 1, totalRetryDelay: 0 }
}

What is the cause of this issue and how can I resolve it?

1

There are 1 answers

0
Luki B. Subekti On

I've found the solution. We can utilize TranslateTextCommand from @aws-sdk/client-translate rather than CreateParallelDataCommand for building the command. Those classes use different input parameter formats. The parameter that I used in the sample program works only for TranslateTextCommand. So, the fix is as follows.

const { TranslateClient, TranslateTextCommand } = require("@aws-sdk/client-translate");

// ...

const params = {
  Text: "StackOverflow is very good.",
  SourceLanguageCode: "en",
  TargetLanguageCode: "id"
};

const command = new TranslateTextCommand(params)

// ...