0

This is my code:

var ddb = new AWS.DynamoDB();

const tableName = 'Table_One';

  let client_id = "a3bdso310";
  console.log(`"${client_id}"`);

  var params = {
      TableName: tableName,
      Key: {
        'client_id': {S: client_id}
      },
  };
  

  ddb.getItem(params, function(err, data) {
      if (err) {
          console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
      } else {
          let str_client_data = JSON.stringify(data.Item.client_data.S);
          let parsed_client_data_str = JSON.parse(str_client_data);
          let parsed_data = JSON.parse(parsed_client_data_str)
          res.send(parsed_data);
      }
  });

I am getting an error when I try to perform a GetItem using DynamoDB SDK: "The provided key element does not match the schema". Whenever I replace {S: client_id} with the actual value like this {S: "a3bdso310"} it works just find. Can the "Key" value not be a variable? What is even weirder is that when I test this locally it works just fine, the issue is only occurring when I deploy my local project to an EC2. So basically when I run this project on my local machine, everything is the same, the only difference I can think of when running it in an EC2 instance is maybe the fact that the EC2 instance has to have permissions to perform operations on my DynamoDB table. But I already gave it permissions, and other operations work fine that don't require a key (e.g. scan). Is there a solution around this?

12
  • you saw this ? stackoverflow.com/questions/36506546/… Commented Apr 27, 2022 at 21:55
  • It looks like a similar issue on the surface, but it is different. His issue is he needed to include the sort key. My issue is that I am using a javascript variable instead of a hardcoded value (which one would assume would be extremely common) and it cannot perform the GetItem, unless I use the hardcoded value. Commented Apr 27, 2022 at 22:00
  • My guess: you have a type issue Commented Apr 27, 2022 at 23:11
  • Using a variable vs. a string constant is irrelevant, so not the cause. It's not a permissions issue as that would not cause the error message you see. I'm not sure I actually believe that if you change {S: client_id} to {S: "a3bdso310"} it works, unless you are not showing us all the relevant code in your post. Fundamentally, I think your table has a composite key (partition key plus sort key) or your partition key is not string type or the partition key is not named client_id or you're actually making API calls against a different table than you think. Commented Apr 27, 2022 at 23:18
  • Thanks for the reply @jarmod, I edited the post to have the full code. The only part I am not showing is that this is wrapped in an express app.get() but that does not make a difference here. I am positive that the table does not have a composite key. Commented Apr 27, 2022 at 23:24

0

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.