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フォーラムとスタックのオーバーフローで多くを検索しました。誰でも私がこの問題を解決するのを手伝ってくれれば幸いです。 CODEを更新し
は--- ---
は、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);
};
上記のコードが書かれ、ラムダ関数でテストされます。
答えをありがとう。 CognitoIdentityProvider.signUpメソッドを、プレーンなJavaScriptオブジェクトとして宣言されているイベント変数でどのように使用することができますか? –
イベントを受け取った後、イベントに割り当てます。もう一つはクリーンアップを忘れていました。更新されたままになります。 – Kannaiyan
ご協力いただきありがとうございます。それは私が部分的に問題を解決するのを助けた。ドキュメンテーションを見て、問題を解決しました。ところで、私は上記のコードを変更して更新しました。ありがとう! –