简体   繁体   English

如何使用无服务器堆栈更新 dynamodb 中的嵌套列表数据?

[英]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
        }
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用无服务器堆栈在 dynamodb 中添加嵌套列表数据? - How do I add nested list data in dynamodb using serverless stack? 如何使用文档客户端更新dynamodb中的嵌套列表数据 - How do I update nested list data in dynamodb using document client 如何使用文档客户端覆盖 dynamodb 中的字符串列表 - How do I overwrite a list of strings in dynamodb using document client 如何在 dynamodb 的嵌套元素中执行 updateItem? - How can I do an updateItem in a nested element in dynamodb? 如何从 a.netlify serverless function 获取 POST 数据? - How do I get the POST data from a netlify serverless function? 如何使用无服务器框架引用不在根文件夹中的函数? - How do I reference a function that is not in the root folder using the Serverless Framework? 如何使用nodejs将数据添加到dynamoDB中的多条记录的列表中? - How to add data to a List, of multiple records in dynamoDB using nodejs? 如何在 lambda nodejs 中进行嵌套的 dynamodb 调用? - How to do nested dynamodb calls in lambda nodejs? 如何使用NodeJS删除dynamoDb中的嵌套项目 - How to delete a nested item in dynamoDb using NodeJS 使用 node.js 对具有嵌套 object 的 dynamoDB 进行条件更新 - Conditional update to dynamoDB with nested object using node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM