0

I am querying dynamodb from lambda function written in node.js env -

Trying to query for table CurrencyPrice where Currency column has value "BLC". When I test my function in lambda console, - it prints until second console log - "querying DB ConsolePrice" and returns a NULL response. It does not print either of the next two console logs and not sure if it is even connecting to DB.

It seems that the code does not go into ddb.query() function at all - had tried putting all loggers in this function but none gets printed.

I have tried checking all possible aws documentation but not able to understand why this function is not getting executed.

My code looks something like below -

var AWS = require ('aws-sdk');
exports.handler = async (event) => 
{ 
  AWS.config.update({region: 'ap-southeast-2'});
  console.log("i am in function");
  // Create DynamoDB service object
  var ddb = new AWS.DynamoDB.DocumentClient();
  var table = 'CurrencyPrice';
  var params = {
    "Select": "ALL_ATTRIBUTES","TableName": "CurrencyPrice",
  };
  console.log("querying DB" + table);
  ddb.query(params, function(err, data) {
    console.log("i am in ddb query");
    if (err) {
      console.error("Unable to query. Error:", JSON.stringify(err, null,2));
    } else {
      console.log(data);   
    }
  });
};

Current result that I am getting in lambda console:

Response:
null
Request ID:"XXXX"
Function Logs:
START RequestId: XXX Version: $LATEST
2019-06-02T13:31:55.189Z    XXXX    INFO    i am in function
2019-06-02T13:31:55.331Z    XXXX    INFO    querying DBCurrencyPrice
2019-06-02T13:31:55.390Z    XXXX    INFO    { Select: 'ALL_ATTRIBUTES', TableName: 'CurrencyPrice' }
END RequestId: XXXX

I expect that at least it prints "Unable to Query" or actual data that it connects to DB and query?

3 Answers 3

2

If you're using async/await you would want to return a promise.

var AWS = require("aws-sdk");
AWS.config.update({ region: "ap-southeast-2" });
var ddb = new AWS.DynamoDB.DocumentClient();

exports.handler = async event => {

  console.log("i am in function");
  // Create DynamoDB service object

  var table = "CurrencyPrice";
  var params = {
    Select: "ALL_ATTRIBUTES",
    TableName: "CurrencyPrice"
  };
  console.log("querying DB" + table);

  return ddb
    .query(params)
    .promise()
    .then((err, data) => {
        console.log("i am in ddb query");
        if (err) {
            console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
        } else {
            console.log(data);
        }
    });
};
Sign up to request clarification or add additional context in comments.

Comments

1

The AWS example

ddbClient.query(params, callback)

shows a synchronous matter. Execution continues without waiting for the callback finishes. That's why you got Null.

You will have to wrap this .query() call into a Promise, so the execution will wait for completion of this callback.

Comments

0

Make query a promise and wait for its fulfilled state.

const promise = await ddb.query(params, function(err, data) {
   console.log("i am in ddb query");
   if (err) {
    console.error("Unable to query. Error:", JSON.stringify(err, null,2));
   } else {
    console.log(data);
    // process your data   
   }
}).promise();
return promise;

1 Comment

Please add an explanation to your code: What does it do, how does it work, and how does it solve OPs problem. Code only answers can lead to cargo cult programming, and you risk getting downvotes.

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.