Currently, I'm utilizing Serverless to build a REST get endpoint. The main goal is to request this endpoint and receive a value from the DynamoDB query (specifically from the body
tag). The issue I am facing is that when this endpoint is invoked, the response comes back as {}
. My assumption is that this happens because the return
is executed before the query data becomes available.
Even though I am implementing the await
keyword when executing the query, I expected to receive the actual data instead of the promise.
This is the code I am using:
import { APIGatewayProxyHandler } from 'aws-lambda';
const AWS = require('aws-sdk');
var dynamoDB = new AWS.DynamoDB.DocumentClient();
export const getorder: APIGatewayProxyHandler = async (event, _context) => {
var orderRefId = +event.queryStringParameters.orderRefId;
var params = {
TableName: 'MYTABLENAME',
KeyConditionExpression: 'orderRefId = :orderRefId',
ExpressionAttributeValues:{
':orderRefId': orderRefId
}
}
let result = await dynamoDB.query(params).promise();
return {
statusCode: 200,
body: JSON.stringify({
order: result.Items[0].body
})
};
}
Do you have any insights on why the return
statement is triggering prior to the result
containing its data?