Environment:
NodeJS used with "aws-sdk": "^2.1692.0" to test DynamoDB operations
Goal:
To fetch an item in an exsiting table called CUSTOMER_LIST which contains a list of items.
When I used putItem method to store data, it was a success with the given schema,
var params = {
TableName: "CUSTOMER_LIST",
Item: {
CUSTOMER_ID: { N: "001" },
CUSTOMER_NAME: { S: "Richard Roe" },
},
};
And when I want to fetch an item(getItem method), the given schema is,
var params = {
TableName: 'CUSTOMER_LIST',
Key: {
"CUSTOMER_ID": { N: "001" },
},
ProjectionExpression: 'CUSTOMER_NAME',
};
but I end up having an error in the console given below
Error ValidationException: The provided key element does not match the schema
The same error is also thrown out for deletion of an item.
Here's a link from the AWS documentation from where the example has been taken.
You have set CUSTOMER_NAME
as the sort key, and in DynamoDB to do a GetItem
you must provide the full primary key (partition and sort).
If you want to do a fetch with just the partition key, you must use Query operation:
var params = {
TableName: 'CUSTOMER_LIST',
KeyConditionExpression: 'CUSTOMER_ID = :id',
ExpressionAttributeValues: {
':id': { N: '001' }
},
ProjectionExpression: 'CUSTOMER_NAME'
};
Then query
instead of getItem
:
dynamodb.query(params, function(err, data) {
if (err) {
console.error("Query failed:", JSON.stringify(err, null, 2));
} else {
console.log("Query succeeded:", JSON.stringify(data.Items, null, 2));
}
});