0

I am trying to scan the Dynamodb table form my following code, can anyone please guide me what is wrong here.

const AWS = require("aws-sdk");
const dynamodb = new AWS.DynamoDB({
  region: "eu-west-1",
  apiVersion: "2012-08-10"
});

exports.handler = async (event, callback) => {
   const params = {
    TableName: "job_Status"
  };
  
  dynamodb.scan(params, (err, data) => {
      
    if (err) {
      console.log(err);
      callback(err);
    } else {
      console.log(data);
      callback(null, data);
      
    }
  });
};

I have given full dynamodb access role to the function but still it gives me the null response. Any idea what can be wrong here?

Response: enter image description here

I tried with dynaomClient which not working too.

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

const db = new AWS.DynamoDB.DocumentClient({
   region : 'eu-west-1' 
});

exports.handler = async (event, callback) => {
   const params = {
    TableName: "job_Status"
  };
  
  db.scan(params, (err, data) => {
      
    if (err) {
      console.log(err);
      callback(err);
    } else {
      console.log(data);
      callback(null, data);
      
    }
  });
};
2

1 Answer 1

1

Your Lambda function is async but your code uses callbacks. By the time the callback is reached, your function has already been terminated because it ran asynchronously. I'd speculate that the null output you see is the return value from the Lambda function, not your console.log.

Replace your call to scan with the following:

try{
  let results = await db.scan(params).promise()
  console.log(results);
} catch(err){
  console.log(err)
}

For more info, check out the AWS documentation about working with promises.

Sign up to request clarification or add additional context in comments.

Comments

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.