[英]How do I update a nested list data in dynamodb using serverless stack?
I have a dynamoDB table that has an Item that includes a user and a List of plans.我有一个 dynamoDB 表,其中包含一个包含用户和计划列表的项目。 It looks like this:它看起来像这样:
Item:
{
user: 'abc123',
plans: [
{
id: 1,
name: 'movies',
category: 'category',
price: 200,
},
{
id: 2,
name: 'fishing',
category: 'category2',
price: 400,
}
]
}
Now, I want to update only id:2's object(name, category, price) in the List.现在,我只想更新列表中 id:2 的对象(名称、类别、价格)。 So I wrote the handler below.所以我在下面写了处理程序。 And there is an error edit error ValidationException: The document path provided in the update expression is invalid for update
in CloudWatch.并且有一个错误edit error ValidationException: The document path provided in the update expression is invalid for update
。
export const processAddPlan = async (event:APIGatewayEvent) => {
const data = JSON.parse(event.body)
const { store, id } = event.queryStringParameters
const params = {
TableName: usersTable,
Key: {
store: store,
id: id,
},
UpdateExpression: 'SET #pl[1] = :plans',
ExpressionAttributeNames: {
'#pl' : 'plans',
},
ExpressionAttributeValues: {
':plans': [
{
'name': data.planName,
'category': data.planCategory,
'price': data.planPrice,
},
],
},
ReturnValues: 'UPDATED_NEW',
}
log.info('params', params)
await dynamoDb.update(params).catch(e => log.info('edit error', e))
return success('edit plan succeeded')
}
I set query params and I tested(send) by postman like this.我设置了查询参数,并像这样通过 postman 测试(发送)。
{
"plans":[
{"planName":"ga2new",
"planCategory": "ttnew",
"planPrice": 5675
}
]
}
You need to use SET
.您需要使用SET
。
SET pl[1] = :plans
As the docs show here:正如这里的文档所示:
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.SET.AddingListElements https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.SET.AddingListElements
The docs for ADD
say ADD
的文档说
The ADD action supports only number and set data types. ADD 操作仅支持数字和集合数据类型。
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.ADD https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.ADD
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.