简体   繁体   English

InvalidParameterType:期望的params.UserAttributes名称为字符串AWS Cognito SDK

[英]InvalidParameterType: Expected params.UserAttributes Name to be a string AWS Cognito SDK

I am totally new to aws cognito sdk. 我对AWS Cognito SDK完全陌生。 I am trying to sign up a user using cognito sdk in Node.js. 我正在尝试在Node.js中使用cognito sdk注册用户。 Whenever I try to run the node.js code, It throws me an error saying 每当我尝试运行node.js代码时,都会抛出错误提示

    { MultipleValidationErrors: There were 5 validation errors:
* InvalidParameterType: Expected params.UserAttributes[0].Name to be a string
* InvalidParameterType: Expected params.UserAttributes[1].Name to be a string
* InvalidParameterType: Expected params.UserAttributes[2].Name to be a string
* InvalidParameterType: Expected params.UserAttributes[3].Name to be a string
* InvalidParameterType: Expected params.UserAttributes[4].Name to be a string

Here is my code. 这是我的代码。

index.js index.js

var AWSCognito = require('aws-sdk');
var AmazonCognitoIdentity = require('amazon-cognito-identity-js');
var CognitoSDK = require('amazon-cognito-identity-js-node');

AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool = CognitoSDK.CognitoUserPool;
AWSCognito.CognitoIdentityServiceProvider.CognitoUser = CognitoSDK.CognitoUser;
AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute = CognitoSDK.CognitoUserAttribute;  
AWSCognito.config.region = 'us-west-2';   
var poolData = {
    UserPoolId : '...', // your user pool id here
    ClientId : '....' // your app client id here
};
var userPool =
     new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
    Username : 'jayanthv', // your username here
    Pool : userPool
};    
//---------------Signing up Users for Your App---------------------   
var attributeList = [];    
var dataEmail = {
    Name : JSON.stringify("email"),
    Value : 'jayanth49@gmail.com' // your email here
};
var dataPhoneNumber = {
    Name : 'phone_number',
    Value : '8326623393' // your phone number here with +country code and no delimiters in front
};
var dataName = {
    Name : 'name',
    Value : 'Jayanth' // your phone number here with +country code and no delimiters in front
};
var dataProfile = {
    Name : 'profile',
    Value : 'SamplePortal' // your phone number here with +country code and no delimiters in front
};
var dataGender = {
    Name : 'gender',
    Value : 'Male' // your phone number here with +country code and no delimiters in front
};
var attributeEmail =
    new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail);
var attributePhoneNumber =
    new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataPhoneNumber);
var attributeName =
    new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataName);
var attributeProfile =
    new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataProfile);
var attributeGender =
    new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataGender);
attributeList.push(attributeEmail);
attributeList.push(attributePhoneNumber);
attributeList.push(attributeName);
attributeList.push(attributeProfile);
attributeList.push(attributeGender);

var cognitoUser;
userPool.signUp('jayanthv', 'J@asdada', attributeList, null, function(err, result){
    if (err) {
        console.log(err);
        return;
    }
    cognitoUser = result.user;
    console.log('user name is ' + cognitoUser.getUsername());
});

I don't really have any idea why this occurs. 我真的不知道为什么会这样。 I have searched a lot in aws forums and stack overflow. 我在aws论坛中搜索了很多内容,并且堆栈溢出。 I would be happy if anyone can help me solve this issue. 如果有人可以帮助我解决此问题,我将非常高兴。

---UPDATED CODE--- -更新的代码-

Followed the below documentation which explains in detail how to use the API calls. 遵循以下文档,该文档详细说明了如何使用API​​调用。

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html

Here is my updated code. 这是我更新的代码。 index.js index.js

var AWSCognito = require('aws-sdk');

AWSCognito.config.region = 'us-west-2';

exports.handler = (event, context, callback) => {

    var params = {
        ClientId: event.ClientId, /* required */
        Password: event.Password, /* required */
        Username: event.Username, /* required */

        UserAttributes: [
            {
                Name: 'email', /* required */
                Value: event.email
            },
            {
                Name: 'gender',
                Value: event.gender
            },
            {
                Name: 'name',
                Value: event.name
            },
            {
                Name: 'phone_number',
                Value: event.phone_number
            },
            {
                Name: 'profile',
                Value: event.profile
            }
            /* more attributes if needed */
        ]
    };

    var userPool = new AWSCognito.CognitoIdentityServiceProvider();
    var responseData = null;
    userPool.signUp(params, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else    {
            console.log(data);
            responseData = data;
        }
                       // successful response
    });


    console.log(responseData);
    callback(responseData, null );
};

The above code is written and tested in a Lambda Function. 上面的代码是在Lambda函数中编写和测试的。

This is how it worked for us, 这就是我们的工作方式,

 //Required parameters in event:  

 var event = {};                                                                                                                                                                                                                                                                                                                                                     
 event.params = {                                                                                                                                                                                          
     ClientId: 'clientid', /* required */                                                                                                                                                
     Password: 'password', /* required */                                                                                                                                                             
     Username: 'username', /* required */                                                                                                                                               

     UserAttributes: [                                                                                                                                                                                     
         {                                                                                                                                                                                                 
             Name: 'email', /* required */                                                                                                                                                                 
             Value: 'emailaddress'                                                                                                                                                           
         },                                                                                                                                                                                                
         {                                                                                                                                                                                                 
             Name: 'family_name',                                                                                                                                                                          
             Value: 'familyname'                                                                                                                                                                         
         },                                                                                                                                                                                                
         {                                                                                                                                                                                                 
             Name: 'given_name',                                                                                                                                                                           
             Value: 'givenname'                                                                                                                                                                        
         },                                                                                                                                                                                                
         {                                                                                                                                                                                                 
             Name: 'phone_number',                                                                                                                                                                         
             Value: '+19999999999'                                                                                                                                                                         
         }                                                                                                                                                                                                 
         /* more attributed if needed */                                                                                                                                                                                  
     ]                                                                                                                                                                                                     
 }; 

// Above event is for reference //以上事件仅供参考

exports.Execute = function(event, callback) {                                                                                                                                                                
    var params = event.params;                                                                                                                                                                                                                                                                                                                                                                              
    cognitoidentityserviceprovider.signUp(params, function(err, data) {                                                                                                                                
        if (err) {                                                                                                                                                                                           
            callback(null, err);                                                                                                                                                                             
        } else {                                                                                                                                                                                             
            callback(null, data);                                                                                                                                                                            
        }                                                                                                                                                                                                    
    });  

I had to remove some of the application specific code, that is not related here. 我必须删除一些与应用程序特定的代码,在这里不相关。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM