skip to Main Content

I am building REST APIs in Nodejs using AWS. I am expecting a response on Postman saying that the
"

Your Bid Must Be Higher than ${auction.highestBid.amount}

"
But instead, I get an Internal Server Error on Postman and the error on AWS Cloudwatch looks like:
enter image description here.
However, I am sending a request on Postman as:
enter image description here
Please help!!

I am Expecting the response as:
Your bid must be higher than

${auction.highestBid.amount} 

The patch request body looks like:
enter image description here
While the create Request looks like:
enter image description here

//placeBid.js

const AWS = require('aws-sdk');
const createError = require('http-errors');
const {getAuctionById} = require('./getAuction');

const dynamodb = new AWS.DynamoDB.DocumentClient();
async function placeBid(req) {

  const { id } = req.pathParameters;
  const { amount } = JSON.parse(req.body);
  const auction = getAuctionById(id);
  if(amount <= auction.highestBid.amount)
  throw new createError.Forbidden(`Your Bid Must Be Higher than ${auction.highestBid.amount}`);

  const params = {
    TableName: 'AuctionsTable',
    Key : {id},
    UpdateExpression : 'set highestBid.amount = :amount',
    ExpressionAttributeValues: {
      ':amount' : amount
    },
    ReturnValues : 'ALL_NEW'
  }

  let updatedAuction;
  try {
    const result = await dynamodb.update(params).promise();
    updatedAuction = result.Attributes;
  } catch (error) {
    console.error(error);
    throw new createError.InternalServerError(error);
  }


  return{
    statusCode : 200,
    body : JSON.stringify(updatedAuction)
  }
}

module.exports.handler = placeBid;

//getAuction.js

const AWS = require('aws-sdk');
    const createError = require('http-errors');
    
    const dynamodb = new AWS.DynamoDB.DocumentClient();
    
    module.exports.getAuctionById = async(id) => {
      let auction;
      try {
        const result = await dynamodb.get({
          TableName : 'AuctionsTable',
          Key : {id}
        }).promise()
        auction = result.Item;
    
      } catch (error) {
        console.error(error);
        throw new createError.InternalServerError(error);
      }
      if(!auction){
        throw new createError.NotFound(`Auction with ID ${id} not found`);
      }
      return auction;
    }
    
    async function getAuction(req) {
      const { id } = req.pathParameters;
      const auction = await getAuctionById(id);
      return{
        statusCode : 200,
        body : JSON.stringify(auction)
      }
    }
    
    module.exports.handler = getAuction


3

Answers


  1. getAuctionById is async. It returns a Promise. You have to await it before using its value:

    const auction = await getAuctionById(id);
    
    Login or Signup to reply.
  2. You’re throwing an error without catching it
    throw new createError.Forbidden(`Your Bid Must Be Higher than ${auction.highestBid.amount}`);, so your Lambda crashes and returns 500 error. That’s expected behavior.

    Just return a valid response instead

    return {
        statusCode : 400,
        body : `Your Bid Must Be Higher than ${auction.highestBid.amount}`
      }
    
    Login or Signup to reply.
  3. If you want to see an example of writing a REST API that uses the AWS SDK for JavaScript (v3) that can invoke AWS Services and return data, see this example in the AWS Code Catelog. Once you successfully build the REST API, you can use Postman to sent requests and view the responses.

    This example performs these tasks:

    • Integrate a React.js web application with AWS services.The React app uses the Rest API (you can also use Postman to view the responses)
    • List, add, and update items in an Aurora table.
    • Send an email report of filtered work items by using Amazon SES.
    • Deploy and manage example resources with the included AWS CloudFormation script.

    https://docs.aws.amazon.com/code-library/latest/ug/aurora_example_cross_RDSDataTracker_section.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search