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?

Chase
  • 93
  • 1
  • 1
  • 7
  • you saw this ? https://stackoverflow.com/questions/36506546/dynamodb-key-element-does-not-match-the-schema – Raphael PICCOLO Apr 27 '22 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. – Chase Apr 27 '22 at 22:00
  • My guess: you have a type issue – hunterhacker Apr 27 '22 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. – jarmod Apr 27 '22 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. – Chase Apr 27 '22 at 23:24
  • The weirdest part is that the getItem works fine on my local development machine but does not work in production on the EC2. – Chase Apr 27 '22 at 23:27
  • What is `ddb`? It cannot be a DocumentClient, given that you're providing the item in the lower-level form. – jarmod Apr 27 '22 at 23:28
  • I just added it to the top of the post. – Chase Apr 27 '22 at 23:29
  • I'd guess you're talking to two different tables, with different key schemas, possibly in different regions (or, less likely, in different AWS accounts). – jarmod Apr 27 '22 at 23:30
  • I am using the same table, account, and region. – Chase Apr 27 '22 at 23:43
  • Worth making sure you are using an up to date AWS JavaScript SDK in all clients. I'd also triple check it's the same table (e.g. retrieve count of items and compare, or get a few items and print them). – jarmod Apr 27 '22 at 23:53
  • @jarmod I figured out my issue. Took me all morning. The variable "client_id" was coming through via request.headers. After viewing AWS logs and trying to print the client_id header, it was appearing as undefined. This is why hardcoding it right before making the dynamo call worked. Because when I hardcoded it I was not relying on a value being passed in via a header. The weird part that I still don't get is, it worked on localhost but not in production (on web server). How weird. Anyway, I fixed the problem by passing the id in via query parameters as opposed to using a header to pass it in. – Chase Apr 28 '22 at 19:06

0 Answers0