"AWSCognito" is undefined

I used the following example to implement AWS Cognito in nodeJS, but I ran into the error "AWSCognito" is not defined no-undef "

Link link: https://github.com/aws/amazon-cognito-identity-js/

Below is my code in App.js file. I used react-app.

    import React, { Component } from 'react';
    import logo from './logo.svg';
    import './App.css';
    import { CognitoUserPool, CognitoUserAttribute, CognitoUser } from 'amazon-cognito-identity-js';

    class App extends Component {
      constructor(props, context){
        super(props, context);
        this.state = {
          items: []
        }
      }
      render() {
        const {items} = this.state;
        return (
          <div className="App">
            <div className="App-header">
              <img src={logo} className="App-logo" alt="logo" />
              <h2>Welcome to React</h2>
            </div>
            <p className="App-intro">
              To get started, edit <code>src/App.js</code> and save to reload.
            </p>
            <button onClick={(e) => this.doRegister(e)}>Register</button>
            { items.map(item => <p>{item.id}</p>) }
          </div>
        );
      }
      doRegister(){
        console.log("Register User");
        var poolData = {
            UserPoolId : 'xxxxxxxxxxx', // Your user pool id here
            ClientId : 'xxxxxxxxxxxxxxxxxx' // Your client id here
        };

        var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
        console.debug(userPool);
      }
    }

    export default App;

      

This is the code in package.json. You can see that the aws-cognito-identity library has already been added.

    {
      "name": "my-app",
      "version": "0.1.0",
      "private": true,
      "dependencies": {
        "amazon-cognito-identity-js": "^1.19.0",
        "react": "^15.6.1",
        "react-dom": "^15.6.1",
        "react-scripts": "1.0.10"
      },
      "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test --env=jsdom",
        "eject": "react-scripts eject"
      },
      "devDependencies": {
        "json-loader": "^0.5.4",
        "webpack": "^3.0.0"
      }
    }

      

Why is it showing AWSCognito undefined. Is there something missing in my AWS Cognito related code?

+3


source to share


1 answer


You need the AWS JavaScript SDK in addition to the Amazon Cognito Identity JavaScript SDK .

npm install aws-sdk --save

      



Then you can import it like:

import * as AmazonCognitoIdentity from 'amazon-cognito-identity-js';

class App extends Component {
  ...
  const poolData = { UserPoolId: 'YOUR_USER_POOL_ID', ClientId: 'YOUR_CLIENT_ID' };
  const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
  ...
}

      

+5


source







All Articles