admin管理员组

文章数量:1287218

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.

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.

Share Improve this question edited Feb 24, 2020 at 6:32 Yashwardhan Pauranik 5,5665 gold badges45 silver badges74 bronze badges asked Feb 11, 2017 at 5:56 Aldwin VerzosaAldwin Verzosa 511 gold badge1 silver badge2 bronze badges 1
  • What do all those console.log calls print? – xtx Commented Feb 11, 2017 at 8:27
Add a ment  | 

2 Answers 2

Reset to default 9

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)

You need to pass both primary key and sort key in the params.

You can find these keys from the table UI.

and you should pass those as parameters when making the request

this.ProjectsModel.delete({pk1:"project#giri-test#appsc", sk1:"metadata#giri-test#appsc"}, (error) => {
            if (error) {
                console.error(error);
            } else {
                console.log("Successfully deleted item");
            }
        });

本文标签: javascriptDynamoDB Validation ExceptionKey element does not match the schemaStack Overflow