简体   繁体   English

如何从node.js访问dynamo db

[英]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. 我想从Node.js访问特定主键值的Amazon Data Server数据。 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. 任何帮助将不胜感激。

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! 希望这可以帮助!

It appears you are sending a variable named params which I don't see where you populate it. 看来你正在发送一个名为params的变量,我看不到你在哪里填充它。

You should be sending element : dynamodb.getItem(element, callback) 你应该发送元素dynamodb.getItem(element,callback)

Looks like there is a typo (case difference) in your UserID (vs UserId). 看起来您的UserID中存在拼写错误(大小写差异)(与UserId相比)。 - Upper case D. Give a try with this change. - 大写D.试试这个改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM