6
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.

5
  • Why do you want a synchronous call? Commented Nov 6, 2017 at 15:14
  • because aws lex doesn't support asynchronous call. Commented Nov 6, 2017 at 15:16
  • 2
    In your return item add the callback to return. Check out examples docs.aws.amazon.com/lex/latest/dg/… Commented Nov 6, 2017 at 15:20
  • Take a look at this developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…. Commented Nov 6, 2017 at 15:21
  • How did the issue get resolved to fetch the data synchronously? Need it for lex @Vikash Commented Apr 27, 2022 at 20:18

2 Answers 2

7

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();
Sign up to request clarification or add additional context in comments.

1 Comment

the first example is missing two ) they belong on lines 14 and 15 between } and ;
-5

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);
      });
}

2 Comments

this call is still by passing the dynamo.scan function and not waiting for dynamo.scan to finish
this doesn't solve the issue in lambda.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.