5
votes

I'm trying to get an item from my DynamoDB but get the following error

ValidationException: The provided key element does not match the schema

The create item piece of the code works. But no the Get item.

Table Info:
Table Name: movieTable
Primary Partition Key: itemID
Primary Sort Key: sortKey

Here's the code for the create and update:

        var fbUserId;
        var params;
        var keyText;
        var attText;
        var valText;
        var dynamodb = null;
        var docClient = null;
        var appId = '405140756489952'; //from facebook
        var roleArn = 'arn:aws:iam::042765862882:role/Verzosa'; //from AWS IAM
        var resultData = null;

        document.getElementById('putThis').onclick = function () {
            dynamodb = new AWS.DynamoDB({ region: 'us-west-2' });
            docClient = new AWS.DynamoDB.DocumentClient({ service: dynamodb });
            keyText = document.getElementById("keyValue").value;
            attText = document.getElementById("attributeText").value;
            valText = document.getElementById("valueText").value;
            console.log("Key Value: ", keyText);
            console.log("Attribute: ", attText);
            console.log("Value: ", valText);
            params = {
                TableName: 'movieTable',
                Item: {
                    itemID: keyText,
                    sortKey: valText
                }
            };
            docClient.put(params, function(err, data){
                          if (err) console.log(err);
                          else
                          {
                          resultData = data;
                          console.log(resultData);

                          }
                          })


        };

    document.getElementById('getThis').onclick = function () {
        dynamodb = new AWS.DynamoDB({ region: 'us-west-2' });
        docClient = new AWS.DynamoDB.DocumentClient({ service: dynamodb });
        keyText = document.getElementById("keyValue").value;
        attText = document.getElementById("attributeText").value;
        console.log("Key Value: ", keyText);
        console.log("Attribute: ", attText);
        params = {
            TableName: 'movieTable',
            Key: {
                itemID: keyText,
            },
            ProjectionExpression: "#a",
            ExpressionAttributeNames: {
                '#a': attText
            }
        };
        docClient.get(params, function (err, data)
                      {
                      if (err)
                      {
                      console.log(err, err.stack);
                      }
                      else
                      {
                      console.log("success, logging data: ");
                      console.log(data);//shows keys
                      console.log("attribute 1 is " + data.Item.sortKey)
                      //var output = data.Item.attribute1;
                      l = document.getElementById("output");
                      l.innerHTML = data.Item.sortKey;
                      }
                      })
    };

Any help would be appreciated.

1
What do all those console.log calls print? - xtx

1 Answers

8
votes

You are getting this error because when using AWS.DynamoDB.DocumentClient.get method, you must specify both hash and sort key of an item. But you have only hash key specified (itemId), and sort key is missing.

Here is how your get params should look like:

  ...
  params = {
      TableName: 'movieTable',
      Key: {
          itemID: keyText,
          sortKey: valText   // <--- sort key added 
      },
      ProjectionExpression: "#a",
      ExpressionAttributeNames: {
          '#a': attText
      }
  };
  docClient.get(params, function (err, data) {
  ...

If you'd like to get a record with a hash key only, without specifying its sort key, you should use query method instead of get:

  ...
  params = {
      TableName: 'movieTable',
      KeyConditionExpression: '#itemID = :itemID',
      ProjectionExpression: "#a",
      ExpressionAttributeNames: {
          '#a': attText,
          '#itemID': 'itemID'
      },
      ExpressionAttributeValues: {
          ':itemID': keyText
      }
  };

  dynamodbDoc.query(params, function(err, data) {
  ...

Be aware that while get method always returns 1 or no records, query can possibly return multiple records, so you would have to revisit your current implementation of get callback (e.g. instead of accessing data.Item you should use data.Items array, see query method docs)