DynamoDB getItem 调用未给出响应 [英] DynamoDB getItem call not giving a response

查看:16
本文介绍了DynamoDB getItem 调用未给出响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照 AWS 教程阅读 AWS Lambda 中的基本 DynamoDB 表.我有一些基本代码,似乎运行正常(我没有看到任何错误记录),但我无法获得任何输出:

const AWS = require('aws-sdk');AWS.config.update({region: 'eu-west-1'});const ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});函数读取数据(){console.log("在 readData() 函数中");变量参数 = {表名:桌子",钥匙: {desk_id":{N:'1'}}};console.log("设置参数");//调用 DynamoDB 从表中读取项目ddb.getItem(参数,函数(错误,数据){console.log("在 getItem 回调函数中");如果(错误){console.log("错误", err);}别的 {console.log("成功", data.Item);}});console.log("已完成通话");}

当我上面的函数被调用时,日志显示输出Set params"和Completed call",但这就像回调函数没有被执行.我是否遗漏了执行流程的某些内容?

我使用的是 Node.js 8.10,并且我相信我已经设置了适当的角色权限(对数据库的完全访问权限).

解决方案

好吧,我想通了,你可以使用

我使用它只是为了确保在使用 lambda 函数之前输入的变量是正确的.

I'm trying to read a basic DynamoDB table in AWS Lambda, following the AWS tutorials. I've got some basic code, that seems to be running OK (I'm not seeing any errors logged), but I can't get any output:

const AWS = require('aws-sdk');
AWS.config.update({region: 'eu-west-1'});
const ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});

function readData(){

console.log("In the readData() function");

var params = {
    TableName: "desks",
    Key: {
        "desk_id": {N:'1'}
    }
};
console.log("Set params");
// Call DynamoDB to read the item from the table
ddb.getItem(params, function(err, data) {
    console.log("In getItem callback function");
    if (err) {
        console.log("Error", err);
    }
    else {
        console.log("Success", data.Item);
    }
});
console.log("Completed call");
}

When my function above is called, the logs show the output "Set params" and "Completed call", but it's like the callback function doesn't get executed. Am I missing something around the execution flow?

Edit: I'm using Node.js 8.10 and I believe I've set up the appropriate role permissions (full access on the database).

解决方案

Ok, so I figured it out, Instead of working with specific API version, you can work with the DocumentClient.

About AWS.DynamoDB.DocumentClient :

The document client simplifies working with items in Amazon DynamoDB by abstracting away the notion of attribute values. This abstraction annotates native JavaScript types supplied as input parameters, as well as converts annotated response data to native JavaScript types.

About the get Function:

Returns a set of attributes for the item with the given primary key by delegating to AWS.DynamoDB.getItem().

Code snippet:

const AWS = require('aws-sdk');

const db = new AWS.DynamoDB.DocumentClient({
   region : 'REGION' 
});


function getItem(){

const params = {
  TableName : 'TABLE-NAME',
  Key: {
    'PRIMARY-KEY':'PRIMARY-KEY-VALUE'
  }
};
    
  db.get(params, (err, data) => {
  if (err){
    console.log("Error:", err);
  } 
  else{
    console.log("Success:", data.Item);
    
  } 
  console.log("Completed call");
});

}

exports.handler = (event) => {
    
 getItem()
 
};

By the way, you can use the Scan operation from DynamoDB UI,The scan operation returns one or more items that match the filter you specified.

I used it just to make sure the variables I entered are correct before hitting the lambda function.

这篇关于DynamoDB getItem 调用未给出响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆