undefined 不是构造函数: AWS.Comprehend, aws JavaScript SDK

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

我正在尝试通过 aws JavaScript SDK 使用 Amazon Comprehend API。但我总是得到

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

' 我做错了什么?非常感谢。

所有其他服务,例如Polly 和 Rekognition 运行良好。

 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;

 }

我刚遇到这个问题。我假设你现在已经解决了它,但只是为了 public 论坛...

根据其中一位贡献者 (https://github.com/aws/aws-sdk-js/issues/2417#issuecomment-446001911) 的说法,Comprehend 和 Comprehend Medical 未在主要 sdk 包中导出。您必须像这样直接导入它:

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

const comprehend = new Comprehend();

或者对于 commonjs

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

var comprehend = new Comprehend();