How to access dynamo db from node.js

I want to access Amazon Data Server data from Node.js by a specific primary key value. Data is available in the form of:

{
  "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"
      }
    },

Every time I make a request with a 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 also tried -

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

Any idea? Any help would be appreciated.

+3
source share
3 answers

You must also pass a range key, if it exists in "Key," for parameters

Example it should be

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

Hope this helps!

+1
source

It seems you are sending a variable called params , which I do not see where you are filling it.

: dynamodb.getItem(, )

0

, UserID (vs UserId) ( ). - D. .

0

All Articles