RequireJS和AWS Cognito-错误“未定义CognitoUserPool”

时间:2018-08-20 03:17:15

标签: amazon-web-services amazon-cognito aws-cognito

我正在尝试使用RequireJS使AWS Cognito正常工作。 我收到一条错误消息,指出未定义CognitoUserPool。

确切的错误消息:

  

jquery.min.js:2 jQuery.Deferred exception:AmazonCognitoIdentity是   未定义ReferenceError:未定义AmazonCognitoIdentity

如果我删除RequireJS并仅从脚本标签加载,则此方法不会出现任何问题,因此我肯定这是RequireJS。

<!doctype html>

<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <meta name="description" content="">
    <meta name="author" content=">

    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="pragma" content="no-cache">

    <title>AWS Cognito Test Interface</title>

    <style>
    </style>

    <link 
      rel="stylesheet" 
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" 
      integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" 
      crossorigin="anonymous">

    <!-- optional entry point for requirejs: "data-main" -->
    <script
      data-main="" 
      src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.min.js">
    </script>


  </head>

  <body>
    <div class="container-fluid">
      <div class="row">
        <div class="mt-4 text-center col-sm-12">
          Amazon Web Services (AWS)
          <br />
          Cognito Test Interface

          <div class="mt-5 offset-sm-3 col-sm-6">
            <form action="#">
              <div class="form-group">
                <label for="email">Email address:</label>
                <input type="email" class="form-control" id="email">
              </div>
              <div class="form-group">
                <label for="pwd">Password:</label>
                <input type="password" class="form-control" id="pwd">
              </div>

              <button type="submit" class="btn btn-primary">Submit</button>
            </form>
          </div>
        </div>
      </div>
    </div>

    <script>
      require.config({
        /* "baseUrl" indicates where in the directory structure the file should load;
        any file that appears in the requirejs "require" or "define" function array
        parameter will prepend this value */
        baseUrl: 'node_modules',
        paths:{
          /* if the first file can't be found or doesn't load, requirejs will attempt to 
          load the next file appearing in the array */
          jquery: '//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min',
          jquery_ui: '//code.jquery.com/ui/1.12.1/jquery-ui.min',
          bootstrap_popper_bundle: '//stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.bundle.min',
          aws_cognito: 'amazon-cognito-identity',
          aws_sdk: '//sdk.amazonaws.com/js/aws-sdk-2.296.0.min'
        },
        callback: function() {
          console.log('"require.config" is set.');
        },
        /* for non-amd js libs; bootstrap_popper_bundle requires jquery */
        shim: {
          'bootstrap_popper_bundle': {
            deps: ['jquery']
          },
          'aws_cognito': {
            deps: ['aws_sdk']
          }
        }
      });

      /* inline javascript w/any specified dependencies */
      require(['jquery'], function($) {
        require(['bootstrap_popper_bundle', 'aws_sdk'], function() {
          require(['aws_cognito'], function() {
            $(document).ready(function() {
              var poolData = { 
                UserPoolId : 'us-east-1_t9oNe9q5Z',
                ClientId : '7piauafodpv9lighooficd0mal'
              };

              var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);

              var attributeList = [];

              var dataEmail = {
                  Name : 'email',
                  Value : 'email@mydomain.com'
              };
              var dataPhoneNumber = {
                  Name : 'phone_number',
                  Value : '+15555555555'
              };
              var attributeEmail = new AmazonCognitoIdentity.CognitoUserAttribute(dataEmail);
              var attributePhoneNumber = new AmazonCognitoIdentity.CognitoUserAttribute(dataPhoneNumber);

              attributeList.push(attributeEmail);
              attributeList.push(attributePhoneNumber);

              userPool.signUp('email@mydomain.com', 'password', attributeList, null, function(err, result){
                  if (err) {
                    console.log(err);
                      return;
                  }
                  cognitoUser = result.user;
                  console.log('user name is ' + cognitoUser.getUsername());
              });
            });
          });
        });
      });
    </script>
  </body>
</html>

0 个答案:

没有答案