node.js - “AWSCognito”未定义

标签 node.js reactjs amazon-cognito

我已经使用以下示例在 nodeJS 中实现 AWS Cognito,但我遇到了错误 “'AWSCognito' 未定义 no-undef”

引用链接:https://github.com/aws/amazon-cognito-identity-js/

以下是我在 App.js 文件中的代码。我用过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;

这是 package.json 中的代码。您可以看到 aws-cognito-identity 库已添加。

    {
      "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"
      }
    }

未定义显示 AWSCognito 的原因。我的代码中是否缺少与 AWS Cognito 相关的任何内容?

最佳答案

您需要 AWS SDK for JavaScript除了Amazon Cognito Identity SDK for JavaScript .

npm install aws-sdk --save

然后,您可以将其导入为:

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);
  ...
}

关于node.js - “AWSCognito”未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44944933/

相关文章:

angularjs - Nodejs 和 Express : How to send multiple Json variable from server to Client through response. 发送()

node.js - 在服务器上处理 react 路由的正确方法?

reactjs - onmouseover 不适用于 React.js

amazon-web-services - AWS Cognito 错误 : 'identityPoolId' failed to satisfy constraint

node.js - AWS cognito : adminUpdateUserAttributes not working and not giving an error , 我错过了什么吗?

javascript - NPM - 脚本 - 它们如何工作?

javascript - 使用 JavaScript 替换字符串中的未知特殊字符

javascript - setState方法导致 "Warning: setState(...): Can only update a mounted or mounting component.."错误

reactjs - 在 React 中将 VideoJs 组件渲染为 HTML

amazon-web-services - AWS Cognito 是否不需要在我的数据库中使用 'users' 表?