DrkStr
DrkStr

Reputation: 1932

Trouble getting data from Amazon DynamoDB

I am having some trouble getting all items from a DynamoDB table. I am following the example from the AWS GitHub repo. Here is the code I have

import {APIGatewayProxyEventV2, APIGatewayProxyResultV2}  from 'aws-lambda';
import {DynamoDB} from 'aws-sdk'

const dynamoClient = new DynamoDB.DocumentClient();

export async function main(
    event: APIGatewayProxyEventV2,
): Promise<APIGatewayProxyResultV2> {
    console.log('event 👉', JSON.stringify(event, null, 2));
    const tableName: string = process.env.TABLE_NAME || ""
    const requesterUserGroup = event.pathParameters?.userGroup || ""
    console.log(`Table Name: ${tableName}`)
    console.log(`User group: ${requesterUserGroup}`)

    if(tableName.length == 0) return sendError("No table name provided", 500)

    if(requesterUserGroup.length == 0) return sendError("Requester user group is empty", 400)

    try {
        const params = {
            TableName: tableName
        };
        const response = await dynamoClient.scan(params)
        // console.log(`response: ${response} `)
        return sendSuccess(response.Items)

    }catch (dbError){
        console.log(dbError)
        return sendError(dbError, 500)
    }
}

function sendSuccess(message: any): APIGatewayProxyResultV2 {
    return {
        statusCode: 200,
        body: JSON.stringify({ message })
    }
}

function sendError(message: string, statusCode: number): APIGatewayProxyResultV2 {
    return {
        statusCode: statusCode,
        body: JSON.stringify({ message })
    }
}

This is the response I am getting from this

{
  "statusCode": 200,
  "body": "{}"
}

I have double-checked the table name and that I have items in the table (a 1000 items!).

Question: Why am I not getting any items from the table?

Upvotes: 1

Views: 399

Answers (1)

TYJ
TYJ

Reputation: 548

You missed .promise(), so it don't await until the request is completed.

const response = await dynamoClient.scan(params).promise();

Upvotes: 5

Related Questions