【问题标题】:How do I add nested list data in dynamodb using serverless stack?如何使用无服务器堆栈在 dynamodb 中添加嵌套列表数据?
【发布时间】:2022-01-07 14:44:15
【问题描述】:

我有一个 dynamoDB 表,其中包含一个包含用户和计划列表的项目。它看起来像这样:

Item: 
{
    user: 'abc123',
    plans: [
        {
            id: 1,
            name: 'movies',
            category: 'category',
            price: 200,
        },
        {
            id: 2,
            name: 'fishing',
            category: 'category2',
            price: 400,
        }
    ]
}

现在,我想添加另一个计划。所以我在下面写了处理程序。 CloudWatch 中出现错误add error ValidationException: Invalid UpdateExpression: An expression attribute value used in expression is not defined; attribute value: :price

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: 'ADD #pl :plans, id :id, name :name, category :category, price :price',
    ExpressionAttributeNames: {
      '#pl' : 'plans',
    },
    ExpressionAttributeValues: {
      ':plans': [
        {
          ':id': uuid.v4(),
          ':name': data.planName,
          ':category': data.planCategory,
          ':price': data.planPrice,
        },
      ],
    },
    ReturnValues: 'ALL_NEW',
  }

  log.info('params', params)

  await dynamoDb.update(params).catch(e => log.info('add error', e))

  return success('add plan succeeded')
}

我设置了查询参数,并像这样由邮递员测试(发送)。

{
    "plans":[
        {"planName":"ga",
         "planCategory": "tt",
         "planPrice": 567
        }
    ]
}

我提到了“向集合中添加元素”。https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html

【问题讨论】:

    标签: node.js amazon-dynamodb serverless-framework


    【解决方案1】:

    只需提供一个值。像这样的东西(未经测试的代码):

     const params = {
        TableName: usersTable,
        Key: {
          store: store,
          id: id,
        },
        UpdateExpression: 'ADD #pl :plans',
        ExpressionAttributeNames: {
          '#pl' : 'plans',
        },
        ExpressionAttributeValues: {
          ':plans': [
            {
              'id': uuid.v4(),
              'name': data.planName,
              'category': data.planCategory,
              'price': data.planPrice,
            }
          ]
        },
    

    【讨论】:

    • 谢谢!我可以修复错误。但是在我修复它add error ValidationException: Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: LIST, typeSet: ALLOWED_FOR_ADD_OPERAND 之后又遇到了另一个错误。我按照这个来修复它。但是 id 没有用。stackoverflow.com/questions/37131606/…
    【解决方案2】:

    这如我所愿。谢谢!

    UpdateExpression: 'SET #pl = list_append(#pl, :plans)',
        ExpressionAttributeNames: {
          '#pl' : 'plans',
        },
        ExpressionAttributeValues: {
          ':plans': [
            {
              'id': uuid.v4(),
              'name': data.planName,
              'category': data.planCategory,
              'price': data.planPrice,
            },
          ],
        },
        ReturnValues: 'ALL_NEW',
      }
    

    【讨论】:

    • 可能你想要id 而不是:id ,对吧?
    • 没错。谢谢^^
    猜你喜欢
    • 1970-01-01
    • 2020-11-19
    • 2018-05-21
    • 2023-04-05
    • 2020-03-09
    • 2020-03-03
    • 2022-06-18
    • 2020-02-09
    • 1970-01-01
    相关资源
    最近更新 更多