Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access dynamo db from node.js

I want access the Data of Amazon Data server from Node.js on specific primary key value. The Data is Available in the form:

{
  "Count": 9862,
  "Items": [
    {
      "Admin": {
        "S": "false"
      },
      "UserId": {
        "S": "e9633477-978e-4956-ab34-cc4b8bbe4adf"
      },
      "Age": {
        "N": "76.24807963806055"
      },
      "Promoted": {
        "S": "true"
      },
      "UserName": {
        "S": "e9633477"
      },
      "Registered": {
        "S": "true"
      }
    },
    {
      "Admin": {
        "S": "false"
      },
      "UserId": {
        "S": "acf3eff7-36d6-4c3f-81dd-76f3a8071bcf"
      },
      "Age": {
        "N": "64.79224276370684"
      },
      "Promoted": {
        "S": "true"
      },
      "UserName": {
        "S": "acf3eff7"
      },
      "Registered": {
        "S": "true"
      }
    },

Everytime when I am Making Request with the code:

app.get('/Mydetails/:tablename/:id', function(req, res){
console.log('Table is ' + req.params.tablename);


var element = {TableName: req.params.tablename, Key:{UserID:{"S": '"'+req.params.id+'"' }}};
 console.log('Id is "' + req.params.id + '"');
dynamodb.getItem(element, function(err, data){
    if(err){
        console.log('Error occurred: '+err);
    }else{
     console.log('succeed');
        res.json(data);
    }
});

then it gives the following error:

**ValidationException: The provided key element does not match the schema.**

However I have tried this also-

var element = {TableName: req.params.tablename, Key:{UserId:{"S": +req.params.id}}};

Any Idea? Any help will be Appreciated.

like image 468
coolhimanshu Avatar asked Mar 27 '26 20:03

coolhimanshu


1 Answers

You have to also pass the range key if it exists in the 'Key', for the parameters

example it should be

Key:{'thePrimaryKey':{ "S": 'primaryKey' }, 'theRangeKey': {'S': 'rangeKey'} }

Hope this helps!

like image 71
ayush narula Avatar answered Mar 30 '26 12:03

ayush narula



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!