mm1975
mm1975

Reputation: 1655

Undefined is not a constructor: AWS.Comprehend, aws JavaScript SDK

I´m trying to use the Amazon Comprehend API via aws JavaScript SDK. But I always get

Uncaught (in promise): TypeError: undefined is not a constructor (evaluating 'new AWS.Comprehend...

' What I´m doing wrong? Thank you so much.

All other services e.g. Polly and Rekognition are working well.

 import * as AWS from 'aws-sdk';

 ....

 getTextAnalysis(textToAnalyze) {

   let awsCredentials = new AWS.Credentials("XXXXXXXXXXX", "XXXXXXXXX");
   let settings = {
       awsCredentials: awsCredentials,
       awsRegion: "us-west-2"
   }

   AWS.config.credentials = settings.awsCredentials;
   AWS.config.region = settings.awsRegion;

   let sentimentAnalysis = new Promise(function (successCallback, errorCallback) {
     var comprehend = new AWS.Comprehend({apiVersion: '2017-11-27'});
     var params = {
          LanguageCode: 'en',
          Text: textToAnalyze
        }

     comprehend.detectSentiment(params, function (error, data) {
         if (error) {
             errorCallback(error)
         } else {
             console.log('comprehend: ' + JSON.stringify(data))
             successCallback(data)
         }
     });

 });

 return sentimentAnalysis;

 }

Upvotes: 1

Views: 1209

Answers (1)

user5602665
user5602665

Reputation:

I just came across this issue. I'm assuming you have solved it by now, but just for the public forum...

According to one of the contributors (https://github.com/aws/aws-sdk-js/issues/2417#issuecomment-446001911) Comprehend and Comprehend Medical aren't exported in the primary sdk bundle. You have to import it directly like so:

import Comprehend from 'aws-sdk/clients/comprehend';

const comprehend = new Comprehend();

or for commonjs

const Comprehend = require('aws-sdk/clients/comprehend');

var comprehend = new Comprehend();

Upvotes: 1

Related Questions