使用Javascript AWS Cognito的Amazon Cognito未定义错误

时间:2016-07-04 12:39:07

标签: javascript node.js amazon-cognito

以下是我的app.js文件。在用户单击提交按钮时的浏览器上,调用方法app.post('/ttt', function (req,res)。我正在尝试使用Amazon Cognito成功注册用户。但是,我收到一条错误消息,指出AWSCognito未定义。我该如何解决这个问题?

我正在关注this教程。

var express = require('express');

var AWSCognito = require('/Applications/XAMPP/xamppfiles/htdocs/my/p/js/aws-cognito-sdk.js');

var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());

var urlencodedParser = bodyParser.urlencoded({ extended: false });
var jsonParser = bodyParser.json();

var path = require('path');
app.use(bodyParser.json());

var http = require('http');
var fs = require('fs');
var ejs = require('ejs');
var formidable = require("formidable");
var util = require('util');
var AWS = require('aws-sdk');


app.post('/ttt', function (req,res){

  AWS.config.region = 'us-east-1'; // Region
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: '...' // your identity pool id here
    });

    AWSCognito.config.region = 'us-east-1';
    AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: '...' // your identity pool id here
    });

    // Need to provide placeholder keys unless unauthorised user access is enabled for user pool
    AWSCognito.config.update({accessKeyId: 'anything', secretAccessKey: ''})

    var poolData = { 
        UserPoolId : 'us-east-1_TcoKGbf7n',
        ClientId : '4pe2usejqcdmhi0a25jp4b5sh3'
    };
    var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);

    var attributeList = [];

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

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

    userPool.signUp('username', 'password', attributeList, null, function(err, result){
        if (err) {
            alert(err);
            return;
        }
        cognitoUser = result.user;
        console.log('user name is ' + cognitoUser.getUsername());
    });

}

1 个答案:

答案 0 :(得分:2)

AWS目前不支持“require”导入Cognito javascript SDK。您可以在手动导出SDK后再试一次吗