How can I run synchronous dynamodb request in lambda?

眉间皱痕 提交于 2020-02-04 11:06:11

问题


const params = {
  TableName: 'item-table',   
  FilterExpression : "#tagname = :itemId",
  ExpressionAttributeNames: {"#tagname": "itemId"},
  ExpressionAttributeValues: {":itemId": "000001"}    
    };
var item ="";
dynamo.scan(params, function(err, data)
{
 if (err) {
    console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
    item = err;
    } else {
        console.log("Query succeeded.");
        data.Items.forEach(function(item) {
          item += item.itemName;
      });
      }
      return item;
});

Scan is not waiting to return the output but going to next step. How can we run synchronous call from lambda to dynamodb.


回答1:


If you really need synchronous scan, you can use one of the ways below:

1. Using Promise resource of JavaScript:

const params = {
    TableName: 'item-table',   
    FilterExpression : "#tagname = :itemId",
    ExpressionAttributeNames: {"#tagname": "itemId"},
    ExpressionAttributeValues: {":itemId": "000001"} };

function scan(params) {
  return new Promise((resolve, reject) => {
    dynamo.scan(params, (err, data) => {
      if (err)
        reject(err);
      else
        resolve(data);
    };
  };
}

async function syncScan() {
  var data;
  try {
    data = await scan(params);
    console.log("Query succeeded.");
  }
  catch (err) {
  console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
  }
        
  return data;
}

syncScan();

2. Using the aws-sdk return objects:

const AWS = require('aws-sdk');
AWS.config.update({ region: 'sa-east-1' });
const docClient = new AWS.DynamoDB.DocumentClient();

async function syncScan() {

  const params = {
    TableName: 'item-table',   
    FilterExpression : "#tagname = :itemId",
    ExpressionAttributeNames: {"#tagname": "itemId"},
    ExpressionAttributeValues: {":itemId": "000001"} 
  };

  const awsRequest = await docClient.scan(params);
  const result = await awsRequest.promise();
  console.log(result.Items); // <<--- Your results are here
}

syncScan();



回答2:


You don't need to run Synchronously, which is not a good idea. Do a callback from scan and do all other logic at the place you are receiving the callback,

const params = {
  TableName: 'item-table',   
  FilterExpression : "#tagname = :itemId",
  ExpressionAttributeNames: {"#tagname": "itemId"},
  ExpressionAttributeValues: {":itemId": "000001"}    
    };
var item ="";
dynamo.scan(params, function(err, data)
{
    callback(err,data);
});

For example, you can refactor code like below,

 scanDynamoDB(function(err,data){
    if (err) {
        console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
        item = err;
        } else {
            console.log("Query succeeded.");
            data.Items.forEach(function(item) {
              item += item.itemName;
          });
        }
 })



function scanDynamoDB(callback)
{
    const params = {
        TableName: 'item-table',   
        FilterExpression : "#tagname = :itemId",
        ExpressionAttributeNames: {"#tagname": "itemId"},
        ExpressionAttributeValues: {":itemId": "000001"}    
          };
      var item ="";
      dynamo.scan(params, function(err, data)
      {
          callback(err,data);
      });
}


来源:https://stackoverflow.com/questions/47140031/how-can-i-run-synchronous-dynamodb-request-in-lambda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!