lambda function returning null for deleting item in DynamoDB

Viewed 110

hi Ive been trying to get my lambda function to delete an item in dynamo db but the function is simply returning null and i have no idea how to even start debugging it, hoping someone here has the knowledge to help

my table has guid as its primary partition key and username as its sort key

heres my code in .js

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

// Initialising the DynamoDB SDK
const documentClient = new AWS.DynamoDB.DocumentClient();

exports.handler = async (event) => {
    const { guid, username } = event
    const params = {
    TableName: "Items", // The name of your DynamoDB table
    Key:{
        "guid": {"S" : guid},
        "username": {"S" : username}
    }
    };
    try {
        // Utilising the scan method to get all items in the table
        documentClient.delete(params, function(err, data) {
        if (err) {
            return("Unable to delete item. Error JSON:", JSON.stringify(err, null, 2));
        } else {
            return("DeleteItem succeeded:", JSON.stringify(data, null, 2));
        }
    });
    }
    catch (e) {
        return {
          statusCode: 500,
          body: e
        };
    }
};

this is the payload for the test event im using in lambda

{
  "guid": "34",
  "username": "newusername"
}
1 Answers

You are using async function handler. So your function probably just finishes before your code actually has a chance to execute.

You can overcome this issue by wrapping your code around new Promise as shown in the docs

Related