InvalidParameterType:期望的params.UserAttributes名称是一个字符串AWS Cognito SDK
问题描述:
我完全陌生于aws cognito sdk。我正在尝试在Node.js中使用cognito sdk注册用户。每当我尝试运行node.js代码时,它会抛出一个错误,说:InvalidParameterType:期望的params.UserAttributes名称是一个字符串AWS Cognito SDK
{ 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 : '[email protected]' // 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', '[email protected]', 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函数进行测试。
答
这是怎么为我们工作,
//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方法吗? –
收到后分配给事件。还有一件事忘了清理。将保持更新。 – Kannaiyan
感谢您的帮助。它帮助我部分解决了这个问题。通过文档解决了这个问题。顺便说一句,我更新了我的代码上面的变化。谢谢 ! –