【问题标题】:Cannot read property of "amount" from the request body无法从请求正文中读取“金额”的属性
【发布时间】:2022-11-26 15:24:28
【问题描述】:

我正在使用 AWS 在 Nodejs 中构建 REST API。我期待邮递员的回应说 “

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

“ 但相反,我在 Postman 上收到内部服务器错误,而 AWS Cloudwatch 上的错误如下所示: enter image description here。 但是,我在 Postman 上发送请求为: enter image description here 请帮忙!!

我期待这样的回应: 您的出价必须高于

${auction.highestBid.amount} 

补丁请求正文如下所示: enter image description here 虽然创建请求看起来像: 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


【问题讨论】:

    标签: node.js amazon-web-services rest aws-lambda


    【解决方案1】:

    getAuctionByIdasync。它返回一个Promise。在使用它的值之前,你必须await它。

    【讨论】:

      【解决方案2】:

      你抛出一个错误而没有发现它 throw new createError.Forbidden(`Your Bid Must Be Higher than ${auction.highestBid.amount}`);,所以你的 Lambda 崩溃并返回 500 错误。这是预期的行为。

      只需返回有效响应即可

      return {
          statusCode : 400,
          body : `Your Bid Must Be Higher than ${auction.highestBid.amount}`
        }
      

      【讨论】:

        【解决方案3】:

        如果您想查看编写使用以下方法的 REST API 的示例适用于 JavaScript 的 AWS 开发工具包 (v3) 可以调用 AWS 服务并返回数据,请参阅AWS 代码目录.成功构建 REST API 后,您可以使用 Postman 发送请求并查看响应。

        此示例执行以下任务:

        • 将 React.js Web 应用程序与 AWS 服务集成。React 应用程序使用 Rest API(您也可以使用 Postman 查看响应)
        • 列出、添加和更新 Aurora 表中的项目。
        • 使用 Amazon SES 发送已过滤工作项的电子邮件报告。
        • 使用包含的 AWS CloudFormation 脚本部署和管理示例资源。

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

        【讨论】:

          猜你喜欢
          • 2016-03-15
          • 1970-01-01
          • 2020-05-16
          • 2018-05-23
          • 2020-11-10
          • 1970-01-01
          • 2021-08-23
          • 2021-12-10
          • 1970-01-01
          相关资源
          最近更新 更多