skip to Main Content

In this code snippet I try to get an item from a table which has partitionKey as a messageId

case "GET /items/{messageId}":
        const { messageId } = event.pathParameters;
        body = await dynamo.send(
          new GetCommand({
            TableName: tableName,
            Key: {
              'messageId': messageId,
            },
          })
        );
        body = body.Items;
        break;

Always getting 500 internal error response. Other calls work

I expect to get the specific item based on partitionKey whihc is messageId.

API that I am trying to call /items/66746629-7220-46fb-a8d4-7059fbf0991f

2

Answers


  1. Chosen as BEST ANSWER

    I was able to resolve it. It was different path parameter(id) instead of (messageId) on API gateway. As well instead of getting body.Items I needed to get the body.Item as it returns single one.

     case "GET /items/{id}":
            body = await dynamo.send(
              new GetCommand({
                TableName: tableName,
                Key: {
                  messageId: event.pathParameters.id,
                },
              })
            );
            if(body.Item){
              body = [body.Item];
            }else{
              body = [];
            }
            
            break;
    

  2. You’re not getting a 500 from DynamoDB, you’re getting it from Lambda thrown back to APIGW.

    Your issue is caused by how you send responses to APIGW, you should handle all scenarios on your code using try/catch and return a response to APIGW that it understands.

    Here is an example of a response in node

    Actions

    • Configure the correct response
    • Add try/catch blocks
    • Add additional logging for testing
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search