TypeError:无法读取属性' CognitoUserPool'未定义的

时间:2017-06-07 15:54:09

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

我正在尝试使用cognito并注册用户。我已经设置了我的游泳池和应用程序,我得到了poolId和客户端ID。有以下代码:

 var AWS = require('aws-sdk'); // Tried 'aws-sdk/dist/aws-sdk' too
  var AWSCognito = require('amazon-cognito-identity-js'); // Tried var cognito = ... too
  var CognitoUserPool = AWSCognito.CognitoUserPool;
  exports.handler  = function(event, context, callback) {
  registerUset();
  callback(null, "some success message");
 }
var registerUset= function(){

var poolData = {
    UserPoolId : 'xxxxxxxxxxxx', // Your user pool id here
    ClientId : 'xxxxxxx' // Your client id here
};  console.log("YEEEEEEEEEEEEEEEEEEEEEEEESSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS");
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var attributeList = [];
var dataEmail = {
    Name : 'email',
    Value : 'xxxxxxxx@brunswicknews.com'
};
var dataPhoneNumber = {
    Name : 'phone_number',
    Value : '55555555'
};
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());
    console.log(cognitoUser);
});
  }

当我在lambda测试控制台中上传并运行上面的代码时,我收到以下错误:

START RequestId: 40f2d39f-4b97-11e7-a9c0-ef84b50cec07 Version: $LATEST
 2017-06-07T15:37:50.315Z   40f2d39f-4b97-11e7-a9c0-ef84b50cec07    
   YEEEEEEEEEEEEEEEEEEEEEEEESSSSSSSSSSSSSSSSSSSSSSS
 2017-06-07T15:37:50.414Z   40f2d39f-4b97-11e7-a9c0-ef84b50cec07    TypeError: 
   Cannot read property 'CognitoUserPool' of undefined
  at registerUset (/var/task/index.js:24:65)
   at exports.handler (/var/task/index.js:8:5)
   END RequestId: 40f2d39f-4b97-11e7-a9c0-ef84b50cec07
   REPORT RequestId: 40f2d39f-4b97-11e7-a9c0-ef84b50cec07   Duration: 3621.75 ms    
   Billed Duration: 3700 ms     Memory Size: 128 MB Max Memory Used: 31 MB  
   RequestId: 40f2d39f-4b97-11e7-a9c0-ef84b50cec07 Process exited before 
   completing request

表示无法读取属性' CognitoUserPool'未定义的。所以意味着CognitoIdentityServiceProvider是未定义的,这很奇怪。 据我所知,我没有遗漏任何内容,我也按照以下链接获取代码示例并设置:https://github.com/aws/amazon-cognito-identity-js

用于我使用的安装和包

> > npm install --save amazon-cognito-identity-js

我也安装了aws-sdk。不知道为什么我收到上述错误。有人可以帮忙吗?

更新:

我在代码开头添加了以下内容:

AWSCognito.config.region = 'us-east-1';

我得到了

TypeError: Cannot set property 'region' of undefined

所以我认为AWSCognito未定义。

更新:我有一个建议,使用webpack转换我的代码,使其兼容旧版本的javascript,并使用import而不是需要。所以这是我的webpack.config.js

var path = require("path");

var DIST_DIR = path.resolve(__dirname, "dist");
module.exports = {
// Example setup for your project:
// The entry module that requires or imports the rest of your project.
// Must start with `./`!
entry: './',
// Place output files in `./dist/my-app.js`
output: {
    path: DIST_DIR,
    filename: 'index.js'
},
module: {
    noParse: [
        /aws\-sdk/,
    ],
    loaders: [
        {
            test: /\.json$/,
            loader: 'json'
        }
    ]
}
};

转换代码并运行后,我得到以下内容:

{
  "errorMessage": "Cannot find module './lib/browser_loader'",
  "errorType": "Error",
  "stackTrace": [
  "Function.Module._load (module.js:417:25)",
  "Module.require (module.js:497:17)",
   "require (internal/module.js:20:19)",
   "Object.<anonymous> (/var/task/index.js:3651:1)",
    "__webpack_require__ (/var/task/index.js:20:30)",
    "webpackUniversalModuleDefinition (/var/task/index.js:149:28)",
    "Object.<anonymous> (/var/task/index.js:156:3)",
    "__webpack_require__ (/var/task/index.js:20:30)",
    "Object.<anonymous> (/var/task/index.js:75:18)"
  ]
  }

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果将来有人需要,我发现了这个问题:

问题是&#34; CognitoIdentityServiceProvider&#34;在实例化中。例如:

new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool

如果我直接从AWSCognito访问CognitoUserPool,则上述工作正常。

这样我甚至不需要使用webpack。