Awaiting a response before triggering

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?

Answer №1

I'm experiencing an issue where the response from this specific endpoint is coming back as an empty object {} when called.

The response can never be just {}. It is probable that it is actually a Promise, so you must handle that accordingly.

Important Reminder

Keep in mind that async functions will always return a promise.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Toggle the visibility of a div by clicking on a label

I am working with three unique labels, each with different ids and three distinct divs also with different ids. <asp:Label ID="CA" runat="server" Font-Bold="False" Font-Names="Arial" Font-Size="10pt" style="padding-top:6px;" ForeColor="#CCCCCC" Height ...

Anticipate commitments during onbeforeunload

Is there a way to trigger a $http.get request when the page is closed? I encountered an issue where promises cannot be resolved because once the final method returns, the page is destroyed. The challenge lies in the fact that onbeforeunload does not wait ...

Amaze the little ones by staggering children using framer-motion with a custom component as the child

Looking to implement a mobile menu with a cool fading effect on the navigation items, but something seems off. The NavLink items all appear simultaneously instead of staggered loading. Initially, considered using 'delay' instead of 'delayCh ...

Create a new tab with a specified URL when a link is clicked

When the 'Terms and Conditions' link is clicked, I want to open a new tab in the same browser with the URL: http://localH:30321/OrchardLocal/TermsAndConditions Here is what I have tried so far: <p>Accept the <a href="~/" target="_blank ...

Best method to run AJAX-retrieved JavaScript code without relying on jQuery

Imagine receiving a response to an AJAX data load request containing a combination of JavaScript and HTML, like this: <script>window.alert('Hello World!');</script> <p>This is a paragraph. Lorem ipsum dolor sit amet...</p> ...

"Are you familiar with delving into the intricacies of this

I've managed to implement a directive that changes a HTML class element when you scroll past a certain point on the page. However, the code is a bit of a mystery to me as I pieced it together from various online sources. I really want to understand ho ...

Guide on streamlining interface initialization within a React project using Typescript

Working on my Typescript project, I consistently utilize an interface within the State of various components: interface Item { selectedValue: string originalSelectedValue: string loading: boolean disabled: boolean isValid?: boolean } ...

Generating distinctive content within the confines of the Selenium WebDriver

Is there a way to generate a unique username value for the signup page username textbox using selenium webdriver instead of hardcoding it? For example: driver.findElement(By.id("username")).sendKeys("Pinklin") ; When "Pinklin" is hardcoded, running the ...

Having difficulty customizing Mui Accordion with Styled Utility implementation

I am having trouble overriding the CSS for an Accordion using Mui styled utility. I am trying to apply a custom CSS class, but there seems to be an underlying class that is causing issues in my code. Here is the Mui class snippet: <div class="MuiPa ...

The functionality of a Vue custom tooltip behaves strangely after clicking the button multiple times

I created this custom tooltip code that automatically closes after 2 seconds when a user clicks on a button, not just hovers over it. Initially, it works perfectly for the first two clicks, but then starts behaving strangely from the third click onwards. ...

To give an element a class in Javascript (without using jQuery) if it is currently hidden

Apologies if this question is not perfect, as I am still learning. I have been struggling to figure out how to add a class to an ID when the class is hidden using pure JavaScript (without jQuery). Below are my attempts so far: function hidekeep() { ...

Getting HTML from Next.js middleware - a step-by-step guide

Is there a way to send the HTTP Status Code 410 (gone) together with a customized HTML message? I want to display the following content: <h1>Error 410</h1> <h2>Permanently deleted or Gone</h2> <p>This page is not foun ...

{ 'Name:UniqueRewrite': { token: 738561, number: 2021.8 } }

Is there a way to extract the token value from this data in Node.js? console.log({'Name:Test': { token: 738561, number: 2021.8 } }) I need to isolate just the token and store it in another variable. ...

Invoking AJAX function post readystatechange

Currently, I am in the process of making an Ajax call to a server and attempting to invoke another function once the response is ready (readystatechanged). As of now, there isn't any serverside code implemented. Surprisingly, Chrome and Firefox encoun ...

Is it possible to extract form data from a div tag based on its class name?

I'm working with some code that looks like this: var iconContainer = document.getElementById('iconContainer'); var icon = iconContainer.getElementsByClassName("item"); for (var i = 0; i < icon.length; i++) { icon[i].addEventListener ...

The problem arises when Angular's $interval function is not recognized

Despite the possibility of this being considered a duplicate, none of the related topics have provided a solution to my simple date count down directive: class Clock { constructor() { this.restrict = 'AC'; this.replace = true ...

Having trouble with ui-sref functionality within a Bootstrap dropdown menu

In the header.html file, I have the following angular code: <div id="navbar" class="navbar-collapse collapse"> <ul class="nav navbar-nav navbar-right"> &l ...

Using SVG graphics as data labels in a HighChart stacked column chart

I am attempting to generate a stacked column chart in Highcharts with SVG images as x-axis labels, similar to the image displayed here: https://i.stack.imgur.com/y5gL1.png I have managed to achieve this with individual data points per label (non-stacked ...

Strip the date after converting from milliseconds to a date

When my server back end sends the time value as milliseconds (1479515722195), I use a library function to convert it to a date format like Sat Nov 19 2016 11:35:22. Now, I need to figure out how to separate the date and time components. I only require th ...

Implementing conditional statements in Puppeteer web scraping

Attempting to extract data from a table... Each country name is wrapped in an <a> tag, however some are not. Unfortunately, when the structure of the HTML changes, the program crashes Code => https://i.sstatic.net/zW3Cd.png Output => https: ...