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
getAuctionById
is async
. It returns a Promise
. You have to await
it before using its value:
const auction = await getAuctionById(id);
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}`
}
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:
https://docs.aws.amazon.com/code-library/latest/ug/aurora_example_cross_RDSDataTracker_section.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.