【问题标题】:AWS-LAMBDA event parameters id does not workAWS-LAMBDA 事件参数 ID 不起作用
【发布时间】:2021-03-11 17:48:54
【问题描述】:

我正在练习 Dynamodb 及其查询。我正在使用无服务器框架。我提出了发布请求,其中全局二级索引分区键是Restaurant's name,排序键是uuid。我的Dynamodb 表看起来像这样Image。我想查询我的全局二级索引,它将采用路径参数 id 并返回给我单个餐厅的输出。

我正在使用邮递员测试我的 api。我使我的全局二级索引分区键值相同,即RESTAURANT,并将我的 event.pathParameters.id 值传递给GSI1SK,但什么也没给我。当我测试 api-gate 的工作方式时,image。当我在我的 lambda 函数中测试时,它说 pathparatmeter 的 id 是undefinedimage。我不明白它在 api-gateway 中是如何工作的,但在 lambda 中却没有。

这是我声明单个餐厅处理程序的无服务器 yml 文件

  getSingleRestaurant:
    handler: handlers/getSingleRestaurant.getSingleRestaurant
    events:
      - http:
          path: restaurant/{id}
          method: get
          cors: true

这是我得到的单一餐厅 lambda 函数

'use strict'

const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
module.exports.getSingleRestaurant = async event => {

  const restaurantId = decodeURIComponent(event.pathParameters.id);

  console.log(restaurantId); // i don't see any thing in cloud watch logs

  const params = {
    TableName: "table name",
    IndexName: "GSI1",
    KeyConditionExpression: 'GSI1PK = :hkey AND GSI1SK = :skey',
    ExpressionAttributeValues: {
      ':hkey': 'RESTAURANT',
      ':skey': restaurantId
    },
    Limit: 1
  };

  try {
    let data = await dynamoDb.query(params).promise();

    return {
      statusCode: 200,
      body: JSON.stringify(data.Items[0])
    };
  } catch (error) {
    return {
      statusCode: 500,
      body: JSON.stringify(error),
    };
  }

};

【问题讨论】:

    标签: amazon-web-services aws-lambda aws-api-gateway dynamodb-queries


    【解决方案1】:

    我的GSI1SK id 以R# 开头。当我在发布请求后测试我的事件参数 id 时,我在 # 之后只得到了R,它没有任何价值。所以我修改了我的发帖请求。

      const params = {
        TableName: "table name",
        IndexName: "GSI1",
        KeyConditionExpression: 'GSI1PK = :hkey AND GSI1SK = :skey',
        ExpressionAttributeValues: {
          ':hkey': 'RESTAURANT',
          ':skey': `R#${restaurantId}`
        },
        Limit: 1
      };

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-21
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 2020-08-25
      • 2019-04-12
      • 2018-07-31
      • 2019-06-19
      相关资源
      最近更新 更多