Home > Software design >  Cannot read property of "amount" from the request body
Cannot read property of "amount" from the request body

Time:11-26

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


CodePudding user response:

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

CodePudding user response:

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}`
  }
  • Related