gpt4 book ai didi

php - DynamoDB 将新 map 添加到列表

转载 作者:可可西里 更新时间:2023-11-01 12:50:32 32 4
gpt4 key购买 nike

我正在尝试将新 map 添加到 DynamoDB 中的列表(评论),但我似乎无法正确处理。下面是表的简单结构:

{
"Comments": [
{
"Body": "This is the first comment",
"Username": "admin"
},
{
"Body": "This is the second comment",
"Username": "Jenny"
},
{
"Body": "This is the third comment",
"Username": "Bob"
}
],
"PostId": "RY28q1AxhR9Qi2VjrDdUdo5bPXBybUg2"
}

PHP 代码:

$response = $ddb->updateItem (
[
'TableName' => 'comments',
'Key' => [
'PostId' => ['S' => $request->input('postid')]
],
"ExpressionAttributeNames" => ["#theList" => "Comments"],
"ExpressionAttributeValues" => [
':addVal' => [
'M' => [
'Username' => ['S' => 'MrSmith'],
'Body' => ['S' => 'Hello this is a comment'],
]
]
],
'UpdateExpression' => 'SET #theList = list_append(:addVal, #theList)',
]);

我收到这个错误:

{
"result": 1,
"error": {
"message": "Error executing \"UpdateItem\" on \"https://dynamodb.us-east-1.amazonaws.com\"; AWS HTTP error: Client error: 400 ValidationException (client): Invalid UpdateExpression: Incorrect operand type for operator or function; operator or function: list_append, operand type: M - {\"__type\":\"com.amazon.coral.validate#ValidationException\",\"message\":\"Invalid UpdateExpression: Incorrect operand type for operator or function; operator or function: list_append, operand type: M\"}",
"status_code": "ValidationException"
}

最佳答案

我也遇到过同样的问题,我的想法在这里和那里都得到了一些解释,这就是最终解决的问题

$res =$this->dynamodb->updateItem([
'TableName' => 'your_table',
'Key' => [
'key1' => ['N' => $detail1,
'key2' => ['S' => $detail2,
"ExpressionAttributeNames" => ["#theList" => "my_list_iten_name"],
"ExpressionAttributeValues" => [
':empty_list' => ['L'=>[]],
':addVal' => [
'L' => [
[
'M' => [
'val1' => ['S' => 'one'],
'val2' => ['S' => 'two!!!'],
]
],
[
'M' => [
'val1' => ['S' => 'one1'],
'val2' => ['S' => 'two2!!!'],
]
]
]
]
],
'UpdateExpression' => 'SET #theList = list_append(if_not_exists(#theList, :empty_list), :addVal)',
]);

关于php - DynamoDB 将新 map 添加到列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33688596/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com