【问题标题】:InvalidParameterType: Expected params.UserAttributes Name to be a string AWS Cognito SDKInvalidParameterType:预期 params.UserAttributes 名称为字符串 AWS Cognito SDK
【发布时间】:2018-04-02 03:33:03
【问题描述】:

我对 aws cognito sdk 完全陌生。我正在尝试在 Node.js 中使用 cognito sdk 注册用户。每当我尝试运行 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

这是我的代码。

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());
});

我真的不知道为什么会发生这种情况。我在 aws 论坛和堆栈溢出中搜索了很多。如果有人能帮我解决这个问题,我会很高兴。

---更新的代码---

按照以下文档详细说明如何使用 API 调用。

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

这是我更新的代码。 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 );
};

以上代码是在 Lambda 函数中编写和测试的。

【问题讨论】:

    标签: javascript node.js aws-sdk aws-cognito


    【解决方案1】:

    这就是我们的工作方式,

     //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 */                                                                                                                                                                                  
         ]                                                                                                                                                                                                     
     }; 
    

    //以上活动供参考

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

    我不得不删除一些应用程序特定的代码,这与这里无关。

    希望对你有帮助。

    【讨论】:

    • 感谢您的回答。我可以知道您如何通过声明为纯 javascript 对象的事件变量使用 CognitoIdentityProvider.signUp 方法吗?
    • 我们在收到事件后分配给它。还有一件事忘了清理。将保持更新。
    • 感谢您的帮助。它帮助我部分解决了这个问题。通过文档解决了这个问题。顺便说一句,我用这些更改更新了我上面的代码。谢谢!
    • 部分解决,新问题是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 2013-03-27
    • 2020-10-03
    • 1970-01-01
    • 2021-02-18
    相关资源
    最近更新 更多