api网关无法使用put调用lambda函数

问题描述 投票:0回答:1

我有一个执行软删除的 lambda 函数,它甚至不调用 lambda 函数,我不知道是否应该进行更多配置。到api网关,但我确实启用了cors并配置了它:

    import { DynamoDBClient, UpdateCommand } from "@aws-sdk/client-dynamodb";

    const client = new DynamoDBClient({ region: 'us-east-1' });
    const { RECORDS_TABLE } = process.env;

export const handler = async (event) => {
  try {
    const id = event.pathParameters?.id;
    
    if (!id) {
      return {
        statusCode: 400,
        headers: {
          "Access-Control-Allow-Origin": "*",
          "Access-Control-Allow-Methods": "OPTIONS,POST,GET,PUT",
          "Access-Control-Allow-Headers": "Content-Type",
        },
        body: JSON.stringify({ message: "ID is required" }),
      };
    }

    const params = {
      TableName: RECORDS_TABLE,
      Key: {
        id: { S: id }
      },
      UpdateExpression: 'SET is_deleted = :true',
      ExpressionAttributeValues: {
        ':true': { BOOL: true }
      },
      ReturnValues: 'ALL_NEW'
    };

    const command = new UpdateCommand(params);
    const result = await client.send(command);
    
    return {
      statusCode: 200,
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Methods": "OPTIONS,POST,GET,PUT",
        "Access-Control-Allow-Headers": "Content-Type",
      },
      body: JSON.stringify({ 
        message: "Successfully soft deleted record",
        record: result.Attributes 
      }),
    };
  

    } catch (error) {
        console.error('Error in soft delete:', error);
        return {
          statusCode: 500,
          headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "OPTIONS,POST,GET,PUT",
            "Access-Control-Allow-Headers": "Content-Type",
          },
          body: JSON.stringify({ 
            message: "Failed to soft delete record", 
            error: error.message 
          }),
        };
      }
    };

我不明白为什么它不起作用,除了 PUT 之外,所有其他方法 POST 和 GET 都可以正常工作。这是我的 api 网关的屏幕截图: enter image description here

amazon-web-services aws-lambda aws-api-gateway
1个回答
0
投票

如果 lambda 函数甚至没有被调用,则意味着 API 网关配置存在问题。您是否使用新的阶段部署了 API 以使其可供访问?我看不到同一路线上的错误或 POST/GET 动词。

© www.soinside.com 2019 - 2024. All rights reserved.