Showing posts with label DynamoDB. Show all posts
Showing posts with label DynamoDB. Show all posts

Wednesday, June 17, 2020

[DynamoDB][Resolved] The provided key element does not match the schema", "code": ValidationExceptio

Source code:
  searchByKeywors(attrName, value){
     var params = {
        TableName: "products",
        Key:{ attrName: value }
     };
    docClient.get(params, function(err, data) {
      if (err) {
          console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
      } else {
          console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
      }
    });
  }


I found it's really strange that the object key of Key value in params doesn't support dynamic value here, this solution is not the best method, if I found another method to do this, I will update later:

Updated code:
  searchByKeywors(attrName, value){
     var params = {
        TableName: "products",
        Key:{ "name": value }
     };
    docClient.get(params, function(err, data) {
      if (err) {
          console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
      } else {
          console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
      }
    });
  }


Wednesday, June 10, 2020

[DynamoDB][Example] An Item example with dynamoDB Json

"S" means String
"N" means Number
"BOOL" neans Boolean
"L" means List
{
  "name": {
    "S": "BACKJOY"
  },
  "isFood": {
    "BOOL": false
  },
  "location": {
    "L": [
      {"N": "2"}
    ]
  },
  "type": {
    "L": [
      {
        "S": "health",
        "S": "daily"}
    ]
  },
  "price": {
    "N": "530"
  },
  "quantity": {
    "N": "1"
  }
}

text format:
{
  "name": "BACKJOY",
  "isFood":false,
  "location": [2],
  "type": ["health"],
  "price": 530,
  "quantity": 1,
}

Wednesday, May 22, 2019

[DynamoDB] Expected params.Item['age'].N to be a string

I am trying to use node.js to insert an record to dynamoDB in lambda, but get this error,

Part of the source :

exports.handler = (event, content, callback) => {
    var params = {
      Item: {
       "UserId": {
            S: "u_"+Math.random()
        },
       "Age": {
            N: event.age
        }
      },
      TableName: "userdata"
     };
     dynamodb.putItem(params, function(err, data) {
       if (err) {
           console.log(err, err.stack); // an error occurred
           callback(null, data);
       }else{
           console.log(data);           // successful response
           callback(null, data);
       }
     });
};

Json data in my request body:

{
    "age":12
}

Solution 1:

DynamoDB expect the N value is a string, but not in number format, so you need to convert the value in number format before you call putItem method. The first
exports.handler = (event, content, callback) => {
    var params = {
      Item: {
       "UserId": {
            S: "u_"+Math.random()
        },
       "Age": {
            N: event.age.toString()
        }
      },
      TableName: "userdata"
     };
     dynamodb.putItem(params, function(err, data) {
       if (err) {
           console.log(err, err.stack); // an error occurred
           callback(null, data);
       }else{
           console.log(data);           // successful response
           callback(null, data);
       }
     });
};

Solution 2:

If you have use apiGateway, you can change the data at body mapping template in "Integration Request", by adding double quotation marks, an example:
#set($inputRoot = $input.path('$'))
{
  "age"    : "$inputRoot.age"
}

Reference:

https://stackoverflow.com/questions/44011192/error-expected-params-itemid-n-to-be-a-string