【发布时间】:2019-10-16 12:46:34
【问题描述】:
我正在使用 AWS 通过 Cognito 用户/IAM 角色和策略对 dynamoDB 进行数据库查询。
听起来我的 DynamoDB 查询参数无效。
我的错误:dynamodb.html:150 ValidationException:一个或多个参数值无效:条件参数类型与架构类型不匹配
我尝试添加:ProjectionExpression: "name",因为它是我的属性之一,但 name 显然是保留字,所以我不能使用它。我也尝试了age,因为它是我拥有的另一个属性,但我得到了与上面相同的初始错误。所以我完全删除了ProjectionExpression:。这是我现在的参数:
var params = {
ExpressionAttributeValues: {
":v1": {
N: 123456788
}
},
KeyConditionExpression: "userId = :v1",
TableName: "user"
};
我不太确定还能如何构建这个框架。请让我知道我缺少什么或者是否有任何好的文档,因为我是 AWS 的新手。提前谢谢!
编辑:
主键为userId,表user的其余属性依次为:age,bio,email,name。
使用 IAM 角色登录和查询
var data2 = {
UserPoolId: 'xxxxxxxx',
ClientId: 'xxxxxxxx',
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(data2);
var cognitoUser = userPool.getCurrentUser();
var userData = {
Username : 'xxxxxxxx',
Pool : userPool
};
var authenticationData = {
Username : 'xxxxxxxx',
Password : 'xxxxxxxx',
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
var cognitoUser2 = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser2.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
var accessToken = result.getAccessToken().getJwtToken(); //user sign-in success and you can get accessToken and IdToken
//POTENTIAL: Region needs to be set if not already set previously elsewhere.
AWS.config.region = 'xxxxxxxx';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId : 'xxxxxxxx', // your identity pool id here
Logins : {
// Change the key below according to the specific region your user pool is in.
// Here you need to map your idToken with your user pool domain for Cognito Identity Pool to generate a Identity with credential.
'cognito-idp.xxxxxxxx.amazonaws.com/xxxxxxxx' : result.getIdToken().getJwtToken()
}
});
//refreshes credentials using AWS.CognitoIdentity.getCredentialsForIdentity()
AWS.config.credentials.refresh((error) => {
if (error) {
console.error(error);
} else {
console.log('Successfully logged!');
// Instantiate aws sdk service objects now that the credentials have been updated. So put your DynamoDB client here
var docClient = new AWS.DynamoDB.DocumentClient({ region: AWS.config.region });
var params = {
ExpressionAttributeValues: {
":v1": {
N: 123456788
}
},
KeyConditionExpression: "userId = :v1",
TableName: "user"
};
docClient.query(params, function(err, data2) {
if (err) {
console.error(err);
}else{
console.log(data2);
}
});
}
});
},
onFailure: function(err) {
alert(err.message || JSON.stringify(err));
},
});
【问题讨论】:
-
你在那个表中的键是什么?
-
您能告诉我们您如何在代码中查询表吗?
-
用一些代码更新/编辑。我只有一个主键,即 userId
-
这个userId的类型是
Number吗?
标签: javascript amazon-web-services parameters amazon-dynamodb amazon-cognito