0

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.

1 Answer 1

1

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));
  }
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot! It worked. Could you plz also provide the link to the documentation to be followed since I've been following the official aws documentation & it didn't work?
what about deletion of an item?
ok I got it. We need to use partition & sort key. Cheers.
You're welcome. I would also recommend you use SDKv3. Please feel free to mark the answer as 'accepted' if it helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.