Ald*_*osa 5 javascript amazon-web-services amazon-dynamodb
我正在尝试从我的 DynamoDB 中获取一个项目,但收到以下错误
ValidationException: 提供的关键元素与架构不匹配
代码的创建项目部分有效。但没有 Get 项目。
Table Info:
Table Name: movieTable
Primary Partition Key: itemID
Primary Sort Key: sortKey
Run Code Online (Sandbox Code Playgroud)
这是创建和更新的代码:
var fbUserId;
var params;
var keyText;
var attText;
var valText;
var dynamodb = null;
var docClient = null;
var appId = '405140756489952'; //from facebook
var roleArn = 'arn:aws:iam::042765862882:role/Verzosa'; //from AWS IAM
var resultData = null;
document.getElementById('putThis').onclick = function () {
dynamodb = new AWS.DynamoDB({ region: 'us-west-2' });
docClient = new AWS.DynamoDB.DocumentClient({ service: dynamodb });
keyText = document.getElementById("keyValue").value;
attText = document.getElementById("attributeText").value;
valText = document.getElementById("valueText").value;
console.log("Key Value: ", keyText);
console.log("Attribute: ", attText);
console.log("Value: ", valText);
params = {
TableName: 'movieTable',
Item: {
itemID: keyText,
sortKey: valText
}
};
docClient.put(params, function(err, data){
if (err) console.log(err);
else
{
resultData = data;
console.log(resultData);
}
})
};
document.getElementById('getThis').onclick = function () {
dynamodb = new AWS.DynamoDB({ region: 'us-west-2' });
docClient = new AWS.DynamoDB.DocumentClient({ service: dynamodb });
keyText = document.getElementById("keyValue").value;
attText = document.getElementById("attributeText").value;
console.log("Key Value: ", keyText);
console.log("Attribute: ", attText);
params = {
TableName: 'movieTable',
Key: {
itemID: keyText,
},
ProjectionExpression: "#a",
ExpressionAttributeNames: {
'#a': attText
}
};
docClient.get(params, function (err, data)
{
if (err)
{
console.log(err, err.stack);
}
else
{
console.log("success, logging data: ");
console.log(data);//shows keys
console.log("attribute 1 is " + data.Item.sortKey)
//var output = data.Item.attribute1;
l = document.getElementById("output");
l.innerHTML = data.Item.sortKey;
}
})
};
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激。
您收到此错误是因为在使用AWS.DynamoDB.DocumentClient.get方法时,您必须同时指定项目的哈希键和排序键。但是您只指定了哈希键 (itemId),并且缺少排序键。
以下是您的 get params 应如下所示:
...
params = {
TableName: 'movieTable',
Key: {
itemID: keyText,
sortKey: valText // <--- sort key added
},
ProjectionExpression: "#a",
ExpressionAttributeNames: {
'#a': attText
}
};
docClient.get(params, function (err, data) {
...
Run Code Online (Sandbox Code Playgroud)
如果您只想获取带有哈希键的记录,而不指定其排序键,则应使用查询方法而不是 get:
...
params = {
TableName: 'movieTable',
KeyConditionExpression: '#itemID = :itemID',
ProjectionExpression: "#a",
ExpressionAttributeNames: {
'#a': attText,
'#itemID': 'itemID'
},
ExpressionAttributeValues: {
':itemID': keyText
}
};
dynamodbDoc.query(params, function(err, data) {
...
Run Code Online (Sandbox Code Playgroud)
请注意,虽然 get 方法总是返回 1 条记录或不返回记录,但查询可能会返回多条记录,因此您必须重新访问 get 回调的当前实现(例如,您应该使用 data.Items 数组而不是访问 data.Item,请参阅查询方法文档)
| 归档时间: |
|
| 查看次数: |
6038 次 |
| 最近记录: |