0

I'm trying to get an item from a DynamoDB table, but this error "ValidationException: The provided key element does not match the schema" keeps happening. These are the parameters i created the table:

TableName : "Users-test",
    KeySchema: [
        { AttributeName: "id", KeyType: "HASH"},  //Partition key
        { AttributeName: "email", KeyType: "RANGE" }  //Sort key
    ],
    AttributeDefinitions: [
        { AttributeName: "id", AttributeType: "S" },
        { AttributeName: "email", AttributeType: "S" }
    ]

And i'm trying to access the data like this:

const docClient = new AWS.DynamoDB.DocumentClient();
const params = {
        TableName: 'Users-test',
        Key:{
            id: "someID",
            email: "[email protected]"
        }
    };
docClient.get(params, function...

I know the record exists on the database and i can filter by id using the aws console, but actually using code i cannot get a record using the primary key, i tried to remove the "email" field from params variable, but it returned the same error.

2
  • Have you tried quoting the attribute names, e.g. "id": "someID"?
    – jingx
    Commented Dec 2, 2020 at 3:11
  • Yeah, i tried this =/ Commented Dec 2, 2020 at 11:34

1 Answer 1

1

You can this,

const config = {
   region: 'eu-central-1'
};
const docClient = new AWS.DynamoDB.DocumentClient(config);
const params = {
    TableName: 'Users-test',
    Key:{
        "someId",
        "[email protected]"
    }
};
docClient.get(params, function...

Also, you can make your function async

const docClient = new AWS.DynamoDB.DocumentClient(config);
const params = {
    TableName: 'Users-test',
    Key:{
        "someId",
        "[email protected]"
    }
};
const result = await docClient.get(params).promise();

Not the answer you're looking for? Browse other questions tagged or ask your own question.