DynamoDB错误:提供的键元素与模式不匹配。

huangapple go评论66阅读模式
英文:

DynamoDB error: The provided key element does not match the schema

问题

我知道关于这个错误有很多类似的问题,但我已经尽力搜索它们,却找不到答案。

我在DynamoDB中创建了一张表,其中有一个分区键名为"token",没有排序键

然后我想在Lambda函数中更新项目(如果不存在则插入新项目)。我的源代码如下:

docClient = new AWS.DynamoDB.DocumentClient({region: region});
let params = {
    TableName: table_name,
    Key: {
        token: token
    },
    UpdateExpression: "Set xxx = :xxx, yyy = :yyy",
    ExpressionAttributeValues: {
        ':xxx': xxx,
        ':yyy': yyy
    }
};
await docClient.update(params).promise();

我已经检查了表名和键是正确的,但我一直收到错误消息:提供的键元素与模式不匹配。

这段代码在开发环境中运行良好,所以我不明白为什么在生产环境中失败了。

由于添加排序键很困难,所以我没有更改表本身。

我尝试创建一个具有相同属性的虚拟记录,但没有成功。

我在docClient.update()之前输出了参数,结果如下:

{
    "TableName": "the_table_name",
    "Key": {
        "token": "this_is_1_token-with-alphabet-number-hyphen_underline"
    },
    "UpdateExpression": "Set xxx = :xxx, yyy = :yyy",
    "ExpressionAttributeValues": {
        ":xxx": "some_string",
        ":yyy": "another_string"
    }
}
英文:

I know there are so many similar questions about the error, but I tried my best to search them only to find no answer to my question.

I have created a table in dynamoDB, with a partition key named "token", no sort key.

Then I want to update item (insert a new one if not exists) in lambda function. My source code is like this:

docClient = new AWS.DynamoDB.DocumentClient({region: region});
let params = {
    TableName: table_name,
    Key: {
        token: token
    },
    UpdateExpression: "Set xxx = :xxx, yyy = :yyy",
    ExpressionAttributeValues: {
        ':xxx': xxx,
        ':yyy': yyy
    }
};
await docClient.update(params).promise();

I have checked that the table name and the key is correct, but I keep getting the error: The provided key element does not match the schema.

This code works good in develop env, so I get confused why it failed in production env.

It is difficult to add a sort key, so I did not change the table itself.

I tried creating a dummy record with the same attributes, it did not work.

I output the params before docClient.update(), it was like this:

{
    "TableName": "the_table_name",
    "Key": {
        "token": "this_is_1_token-with-alphabet-number-hyphen_underline"
    },
    "UpdateExpression": "Set xxx = :xxx, yyy = :yyy",
    "ExpressionAttributeValues": {
        ":xxx": "some_string",
        ":yyy": "another_string"
    }
}

答案1

得分: 2

我写这封信是基于我对你的问题的期待,我假设你正在使用AWS.DynamoDB.DocumentClient,它期望你的值采用本机JSON格式。所以我假设你正在传递低级别的DynamoDB-JSON格式,如下所示:

Key: {
        token: {"S":"some-token"}
    },

然而,你应该使用以下格式:

Key: {
        token: "some-token"
    },

如果我的假设不正确,下一步检查我会做的是数据类型,请确保你为token传递了正确的数据类型,因为DocumentClient会直接从JavaScript中推断数据类型。

接下来,我会进行DescribeTable API调用,并查看输出中的键,确保键名周围没有多余的前导/尾随空格,例如: tokentoken

英文:

I'm writing this as an assumption awaiting your response to my question. As you are using the AWS.DynamoDB.DocumentClient it expects your values to be in native JSON format. So I assume you are passing in the low level DynamoDB-JSON format like below:

Key: {
        token: {"S":"some-token"}
    },

Whereas you should be using the following:

Key: {
        token: "some-token"
    },

If my assumption is incorrect, the next check I would do is the data type, be sure you are passing in the correct data type for token as the DocumentClient infers the data type directly from javascript.

Next, I would do a DescribeTable API call and look at the Keys in the output, be sure there is no trailing/leading white space around the key name, for example: token or token .

huangapple
  • 本文由 发表于 2023年6月22日 18:21:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定