aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.42k stars 2.12k forks source link

Predictions throws an error: Cannot read property 'identityPoolId' of undefined at Credentials #5412

Closed rpostulart closed 4 years ago

rpostulart commented 4 years ago

When I use this code:

import Amplify from "@aws-amplify/core"
import Predictions, {
  AmazonAIPredictionsProvider,
} from "@aws-amplify/predictions"
import awsconfig from "../aws-exports"

Amplify.configure(awsconfig)
Predictions.addPluggable(new AmazonAIPredictionsProvider())

and run this function:

async function getLanguage(text) {
    const data = await Predictions.interpret({
      text: {
        source: {
          text: text,
        },
        type: "LANGUAGE",
      },
    })
      .then(result => {
        return result.textInterpretation.language
      })
      .catch(err => console.log(err))

    return data
  }

Then I receive this error

Uncaught (in promise) TypeError: Cannot read property 'identityPoolId' of undefined
    at Credentials.<anonymous> (Credentials.js:197)
    at step (Credentials.js:43)
    at Object.next (Credentials.js:24)
    at Credentials.js:18
    at new Promise (<anonymous>)
    at ../node_modules/@aws-amplify/core/lib-esm/Credentials.js.__awaiter (Credentials.js:14)
    at Credentials.../node_modules/@aws-amplify/core/lib-esm/Credentials.js.Credentials._setCredentialsForGuest (Credentials.js:189)
    at Credentials.../node_modules/@aws-amplify/core/lib-esm/Credentials.js.Credentials.set (Credentials.js:398)
    at Auth.js:1232

when I replace the import statements with this:

import Amplify from "@aws-amplify/core"
import Predictions, {
  AmazonAIPredictionsProvider,
} from "@aws-amplify/predictions"
import awsconfig from "../aws-exports"

Amplify.configure(awsconfig)
Amplify.addPluggable(new AmazonAIPredictionsProvider())

Then I receive this error:

Predictions.js:124 Uncaught (in promise) Error: More than one or no providers are configured, Either specify a provider name or configure exactly one provider
    at Predictions.../node_modules/@aws-amplify/predictions/lib-esm/Predictions.js.Predictions.getPluggableToExecute (Predictions.js:124)
    at Predictions.../node_modules/@aws-amplify/predictions/lib-esm/Predictions.js.Predictions.interpret (Predictions.js:99)
    at _callee3$ (admin-editquestion.js:248)
    at tryCatch (runtime.js:45)
    at Generator.invoke [as _invoke] (runtime.js:274)
    at Generator.prototype.<computed> [as next] (runtime.js:97)
    at asyncGeneratorStep (asyncToGenerator.js:3)
    at _next (asyncToGenerator.js:25)
    at asyncToGenerator.js:32
    at new Promise (<anonymous>)
    at asyncToGenerator.js:21
    at _getLanguage (admin-editquestion.js:31)
    at getLanguage (admin-editquestion.js:31)
    at _callee2$ (admin-editquestion.js:144)
    at tryCatch (runtime.js:45)
    at Generator.invoke [as _invoke] (runtime.js:274)
    at Generator.prototype.<computed> [as next] (runtime.js:97)
    at asyncGeneratorStep (asyncToGenerator.js:3)
    at _next (asyncToGenerator.js:25)
../node_modules/@aws-amplify/predictions/lib-esm/Predictions.js.Predictions.getPluggableToExecute @ Predictions.js:124
../node_modules/@aws-amplify/predictions/lib-esm/Predictions.js.Predictions.interpret @ Predictions.js:99
_callee3$ @ admin-editquestion.js:248
tryCatch @ runtime.js:45
invoke @ runtime.js:274
prototype.<computed> @ runtime.js:97
asyncGeneratorStep @ asyncToGenerator.js:3
_next @ asyncToGenerator.js:25
(anonymous) @ asyncToGenerator.js:32
(anonymous) @ asyncToGenerator.js:21
_getLanguage @ admin-editquestion.js:31
getLanguage @ admin-editquestion.js:31
_callee2$ @ admin-editquestion.js:144
tryCatch @ runtime.js:45
invoke @ runtime.js:274
prototype.<computed> @ runtime.js:97
asyncGeneratorStep @ asyncToGenerator.js:3
_next @ asyncToGenerator.js:25
Promise.then (async)
asyncGeneratorStep @ asyncToGenerator.js:13
_next @ asyncToGenerator.js:25
Promise.then (async)
asyncGeneratorStep @ asyncToGenerator.js:13
_next @ asyncToGenerator.js:25
(anonymous) @ asyncToGenerator.js:32
(anonymous) @ asyncToGenerator.js:21
_handleCreate @ admin-editquestion.js:31
handleCreate @ admin-editquestion.js:31
onClick @ admin-editquestion.js:796
callCallback @ react-dom.development.js:337
invokeGuardedCallbackDev @ react-dom.development.js:386
invokeGuardedCallback @ react-dom.development.js:441
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:455
executeDispatch @ react-dom.development.js:585
executeDispatchesInOrder @ react-dom.development.js:610
executeDispatchesAndRelease @ react-dom.development.js:714
executeDispatchesAndReleaseTopLevel @ react-dom.development.js:723
forEachAccumulated @ react-dom.development.js:695
runEventsInBatch @ react-dom.development.js:740
runExtractedPluginEventsInBatch @ react-dom.development.js:881
handleTopLevel @ react-dom.development.js:5804
batchedEventUpdates$1 @ react-dom.development.js:24400
batchedEventUpdates @ react-dom.development.js:1416
dispatchEventForPluginEventSystem @ react-dom.development.js:5895
attemptToDispatchEvent @ react-dom.development.js:6011
dispatchEvent @ react-dom.development.js:5915
unstable_runWithPriority @ scheduler.development.js:697
runWithPriority$2 @ react-dom.development.js:12150
discreteUpdates$1 @ react-dom.development.js:24416
discreteUpdates @ react-dom.development.js:1439
dispatchDiscreteEvent @ react-dom.development.js:5882

this is my package.json:

{
    "name": "gatsby-starter-default",
    "private": true,
    "description": "A simple starter to get up and developing quickly with Gatsby",
    "version": "0.1.0",
    "author": "Kyle Mathews <mathews.kyle@gmail.com>",
    "dependencies": {
        "@aws-amplify/api": "^3.1.7",
        "@aws-amplify/auth": "^3.2.4",
        "@aws-amplify/core": "^3.2.4",
        "@aws-amplify/datastore": "^2.0.8",
        "@aws-amplify/predictions": "^3.1.7",
        "@aws-amplify/storage": "^3.1.7",
        "array-move": "^2.2.1",
        "aws-amplify-react": "^3.1.8",
        "bootstrap": "^4.4.1",
        "gatsby": "^2.19.45",
        "gatsby-cli": "^2.11.8",
        "gatsby-image": "^2.2.44",
        "gatsby-plugin-google-analytics": "^2.2.2",
        "gatsby-plugin-manifest": "^2.2.48",
        "gatsby-plugin-offline": "^3.0.41",
        "gatsby-plugin-react-helmet": "^3.1.24",
        "gatsby-plugin-sharp": "^2.4.13",
        "gatsby-source-filesystem": "^2.1.56",
        "gatsby-source-giphy-random": "^1.0.4",
        "gatsby-transformer-sharp": "^2.3.19",
        "prop-types": "^15.7.2",
        "react": "^16.12.0",
        "react-bootstrap": "^1.0.0",
        "react-bootstrap-table-next": "^4.0.0",
        "react-bootstrap-table2-filter": "^1.3.1",
        "react-bootstrap-table2-paginator": "^2.1.2",
        "react-dom": "^16.12.0",
        "react-helmet": "^5.2.1",
        "react-spring": "^8.0.27"
    },
    "devDependencies": {
        "ini": "^1.3.5",
        "inquirer": "^6.5.1",
        "prettier": "^1.19.1"
    },
    "keywords": [
        "gatsby"
    ],
    "license": "MIT",
    "scripts": {
        "build": "gatsby build",
        "develop": "gatsby develop",
        "format": "prettier --write \"**/*.{js,jsx,json,md}\"",
        "start": "npm run develop",
        "serve": "gatsby serve",
        "clean": "gatsby clean",
        "test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1",
        "amplify-modelgen": "node amplify/scripts/amplify-modelgen.js",
        "amplify-push": "node amplify/scripts/amplify-push.js"
    },
    "repository": {
        "type": "git",
        "url": "https://github.com/gatsbyjs/gatsby-starter-default"
    },
    "bugs": {
        "url": "https://github.com/gatsbyjs/gatsby/issues"
    }
}
ashika01 commented 4 years ago

@rpostulart Could you share your aws-exports file with secrets grayed out?

rpostulart commented 4 years ago

// WARNING: DO NOT EDIT. This file is automatically generated by AWS Amplify. It will be overwritten.

const awsmobile = { "aws_project_region": "eu-west-1", "aws_appsync_graphqlEndpoint": "https://****.appsync-api.eu-west-1.amazonaws.com/graphql", "aws_appsync_region": "eu-west-1", "aws_appsync_authenticationType": "AWS_IAM", "aws_cognito_identity_pool_id": "eu-west-1:c**", "aws_cognito_region": "eu-west-1", "aws_user_poolsid": "eu-west-1***", "aws_user_pools_web_client_id": "**", "oauth": {}, "predictions": { "interpret": { "interpretText": { "region": "eu-west-1", "proxy": false, "defaults": { "type": "LANGUAGE" } } } } };

export default awsmobile;

vaterom commented 4 years ago

I am also having problems with the comprehend API from a vue application. (400 bad request error) throwing asyncToGenerator.js?1da1:6 Uncaught (in promise) Not authorized, did you enable Interpret Text on predictions category Amplify CLI? try: amplify predictions add.

** Have already added the predictions category and checked in my cloud formation logs that everything has been provisioned.

EDIT

I have managed to get it working for Auth users by updgrading both the vue plugin and amplify to the latest stable releases, however users that are associated to certain groups are not inheriting the permissions to make the calls to comprehend.

**

I have followed similar steps above with main.js.

import Amplify, * as AmplifyModules from "aws-amplify"; import { AmplifyPlugin } from "aws-amplify-vue"; import awsconfig from "./aws-exports";

Amplify.configure(awsconfig); import { AmazonAIPredictionsProvider } from '@aws-amplify/predictions' Amplify.addPluggable(new AmazonAIPredictionsProvider())

and vue component.

import Predictions from '@aws-amplify/predictions';

interpretText: async function() {
  await Predictions.interpret({
    text: {
      source: {
        text: this.inputText,
        language: "en-US"
      },
      type: "SENTIMENT"
    }
  }).then(response => {
    console.log(response);
  });
  return;
},
rpostulart commented 4 years ago

I have added predictions via the amplify cli and it only happend when I upgraded to the latest amplify modular versions. So yes I am up to date. With version 2.7.0 it worked

Amplifiyer commented 4 years ago

@rpostulart, can you try importing Auth from @aws-amplify/auth and call Auth.configure(awsexports) on it, OR alternatively try changing your Amplify import from @aws-amplify/core to import Amplify from 'aws-amplify' and let us know if either or both work?

With the modularization, we have removed importing all Amplify categories by default in your app. Guest access/unauthorized access requires Auth category to be properly bootstrapped and configured. If you are only importing specific categories, Auth is required to be imported for Guess access as mentioned in the Analytics category https://docs.amplify.aws/lib/analytics/getting-started/q/platform/js#using-modular-imports I'll add more details to this section and add in other categories too.

rpostulart commented 4 years ago

Thanks @Amplifiyer !

This (@aws-amplify/auth and call Auth.configure(awsexports) did the job.

conary-h commented 4 years ago

his (@aws-amplify/auth and call Auth.configure(awsexports) did the job

Like @rpostulart said, this fixed it for me.

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.