DynamoDb PutItem 似乎正确,但 ValidationException:提供的关键元素与模式不匹配

DynamoDb PutItem seems correct, but ValidationException: The provided key element does not match the schema

我知道之前有人问过这个问题,根据这些答案,我很确定我的回答是正确的。在这一点上,我真的觉得我缺少一些非常基本的东西。

DynamoDB 连接器已设置:

const AWS = require('aws-sdk');

// set up dynamoDB
const dynamoDb = new AWS.DynamoDB.DocumentClient();

我在无服务器中定义了一个 DynamoDB table 键:

CampaignStreamDynamoDbTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        ProvisionedThroughput:
          ReadCapacityUnits: 5
          WriteCapacityUnits: 5
        AttributeDefinitions:
          - AttributeName: "id"
            AttributeType: "S"
          - AttributeName: "campaign_id"
            AttributeType: "S"
        KeySchema:
          - AttributeName: "id"
            KeyType: "HASH"
          - AttributeName: "campaign_id"
            KeyType: "RANGE"
        StreamSpecification:
          StreamViewType: "NEW_AND_OLD_IMAGES"
        TableName: ${self:provider.environment.FS_CAMPAIGN_STREAM_TABLE}

我有一个更新 table 的函数,它同时采用主键和范围键(第三个参数是我更新测试所用的状态):

let update_test_status = async (test_id, campaign_key, test_status) => {

console.info("Updating bulk test " + test_id + " with campaign_key "+campaign_key+" to " + test_status);
    let b = true;
    const timestamp = new Date().getTime();

    const update_params = {
        TableName: process.env.FS_CAMPAIGN_STREAM_TABLE,
        Key: {
            id: test_id,
            campaign_key: campaign_key
        },
        ProjectionExpression: "#s",
        ExpressionAttributeNames: {"#s": "status"},
        UpdateExpression: "set #s=:s, updated_at=:u",
        ExpressionAttributeValues: {
            ":s": test_status,
            ":u": timestamp,
        },
        ReturnValues: "UPDATED_NEW"
    };

    try {
        let results = await dynamoDb.update(update_params).promise();

    } catch (error) {
        console.error("Could not update the record: " + error);
        test_status = "error";
        console.error("Links processed, but error updating record in DB");
        b = false;

    }
    return b;

};
    
        try {
            let results = await dynamoDb.update(update_params).promise();
    
        } catch (error) {
            console.error("Could not update the record: " + error);
            test_status = "error";
            console.error("Links processed, but error updating record in DB");
            b = false;
    
        }
        return b;
    };

我这样调用这个函数:

await update_test_status(item.id.S, item.campaign_key.S, "processing")

其中 item 是 DynamoDB 记录

备注:

我已经验证了 id 和 key 都被传递给了更新方法。 我已经尝试了带引号和不带引号的 KEY 参数

在您的 table 定义中,您将其称为 campaign_id,但在您稍后的代码中,您将其称为 campaign_key

看到错误时,您应该始终双重(和三次)检查哈希和范围键的名称和类型是否与定义中的相同。