未被捕获的ReferenceError:未定义AWSCognito

问题描述

我正在尝试使用浏览器脚本方法连接AWS Cognito。 使用来自AWS Blog https://aws.amazon.com/blogs/mobile/accessing-your-user-pools-using-the-amazon-cognito-identity-sdk-for-javascript/的以下代码获取未捕获的ReferenceError:未定义AWSCognito 。错误的摘要。

enter image description here

**

<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8">
    <!-- Javascript SDK-->
 
  <script src="https://sdk.amazonaws.com/js/aws-sdk-2.410.0.min.js"></script> 

 
  </head>

    
  <body>

    <h1 class="h3 mb-3 font-weight-normal" id="titleheader">Register an Account</h1>
    
    <input type="personalname" class="form-control" id="personalnameRegister" placeholder="Name" pattern=".*" required>
    <input type="email" class="form-control" id="emailInputRegister" placeholder="Email" pattern=".*" required>
    <input type="password" class="form-control" id="passwordInputRegister" placeholder="Password" pattern=".*" required>
    <!-- <input type="password" class="form-control" id="confirmationpassword" placeholder="Confirm Password" pattern=".*" required> -->
    <button id="mainbutton" class="btn btn-lg btn-primary btn-block" type="button" onclick="registerButton()" >Register</button>

<script type="text/javascript">
// Amazon Cognito creates a session which includes the id,access,and refresh tokens of an authenticated user.
function registerButton() {

username = document.getElementById("emailInputRegister").value;
password =  document.getElementById("passwordInputRegister").value; 

var authenticationData = {
        Username : username,<!--         Password : password,-->
    };
 var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
    var poolData = { UserPoolId :<POOLID>,ClientId :<clientId>
    };
    var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
    <!-- var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData); -->
    Console.log(userPool)
    var userData = {
        Username : document.getElementById("emailInputRegister").value,Pool : userPool
    };
    
     var cognitoUser = new AWSCognito.AmazonCognitoIdentity.CognitoUser(userData);
    cognitoUser.authenticateUser(authenticationDetails,{
        onSuccess: function (result) {
            var accessToken = result.getAccessToken().getJwtToken();

            /* Use the idToken for Logins Map when Federating User Pools with identity pools or when passing through an Authorization Header to an API Gateway Authorizer */
            var idToken = result.idToken.jwtToken;
        },onFailure: function(err) {
            alert(err);
        },}); 
}   
</script> 
</body> 
</html>

**

解决方法

您需要在head元素中包含amazon-cognito js文件。您可以检查当前控制台以查看当前尚未定义AWSCognito。

<script src="https://rawgit.com/aws/amazon-cognito-identity-js/master/dist/aws-cognito-sdk.min.js"></script>
<script src="https://rawgit.com/aws/amazon-cognito-identity-js/master/dist/amazon-cognito-identity.min.js"></script>

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...