问题
I created a table 'user_info' in DynamoDB with one primary hash key 'user_id'(String), no range key. Then I created 2 AWS lambda functions to insert and query the items. I can insert items into the table, but when I query the table, it returns:
ValidationException: The provided key element does not match the schema.
My query function :
var params = {
Key: {
user_id:{
S: "usr1@s.com"
}
},
TableName: 'user_info',
ProjectionExpression: 'password'
};
dynamodb.getItem(params,
function(err, data) {
if (err) {
console.log("get item err." + err);
context.done('error','getting item from dynamodb failed: '+err);
}
else {
console.log('great success: '+JSON.stringify(data, null, ' '));
context.succeed('created user ' + event.user_id + ' successfully.');
}
});
I keep getting this exception:
ValidationException: The provided key element does not match the schema
Since
1) I have only one hash primary key.
2)user_id is defined as String. I really don't know why there is a mismatch error.
回答1:
To clarify further on why this is happening, you were originally using the DynamoDB Document Client, which eliminates to need to explicitly label your attributes as "String" (S) or "Number" (N) etc. Therefore, your original code would have worked with
var doc = require('dynamodb-doc');
var dynamodb = new doc.DynamoDB();
var params = {
Key: {
user_id: "usr1@s.com"
},
TableName: 'user_info',
ProjectionExpression: 'password'
};
Note the "S" wrapping the value of "user_id" is removed from the above code. Later on, you switched back to the low-level javascript sdk by using 'aws-sdk', so your code with "S" label ended up working.
回答2:
At last, I found out the answer. It's not about the format of params, but with the code before it, which I did not post in my question. When I replace
var doc = require('dynamodb-doc');
var dynamodb = new doc.DynamoDB();
with
var doc = require('aws-sdk');
var dynamodb = new doc.DynamoDB();
the exception disappears.
回答3:
(Different Scenario than OP)
This same error was happening to me when I was trying to query by just a HASH key when my table had both a HASH and SORT Key. I removed the unused SORT key as it wasn't needed for me and it resolved my issue.
来源:https://stackoverflow.com/questions/31889891/validationexception-the-provided-key-element-does-not-match-the-schema