将字符串添加到dynamodb表中的列表中 [英] Add a string to a list in a dynamodb table

查看:82
本文介绍了将字符串添加到dynamodb表中的列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在nodejs express应用程序中使用dynamodb,我有一个特定的表,该表的字段只是空列表,我想在列表中附加一个字符串.

So I have been working with dynamodb in a nodejs express app and I have a specific table that has a field which is just empty lists and I want to append a string to the list.

表名称为"dev_entrants",这是表的示例:

The table name is "dev_entrants" and here is an example of the table:

----------------------------------------
primary Key           Sort Key    
eventID            | eventType  | entrants
----------------------------------------
Qual-919-w5wm1xhnw | Qual       | []

----------------------------------------

因此,我收到一个发布请求,然后通过express路由它,它涉及到一个函数,该函数在进行类型检查以及所有尝试使用以下方法向表中添加内容之后:

So I receive a post request and then route it through express and it comes to a function where after doing type checks and all that I try to add stuff to my table with:

import AWS from 'aws-sdk';

const docClient = new AWS.DynamoDB.DocumentClient({region: 'us-west-1'});

const db = {
  docClient
};

...

const entrantsParams = {
    'TableName' : 'dev_entrants',
    'Key': {
      'eventID' : 'Qual-919-w5wm1xhnw',
    },
    'UpdateExpression' : "SET #attrName = list_append(#attrName, :attrValue)",
    'ExpressionAttributeNames' : {
      '#attrName' : 'entrants'
    },
    'ExpressionAttributeValues' : {
      ':attrValue' : ['joe'],
    }
  };

  const updateEntrantsPromise = db.docClient.update(entrantsParams).promise();

(在本示例中,我已将变量替换为它们代表的字符串)

(For the purpose of this example I have replaced variables with the strings they represent)

我已经花了6个小时左右的时间阅读不同的文档,以及在堆栈溢出中试图找到答案.

I have spent 6 hours or so reading through different documentation, as well as on stack overflow trying to find the answer.

我得到的当前错误是所提供的关键元素与架构不匹配.如果删除attrValue周围的括号,则会得到错误的操作数类型.我知道该键存在于表中,因为我从那里复制并粘贴了它.另外,我还成功地从另一个函数向表中添加了内容,因此我的连接工作正常.有人可以帮我吗?

The current error i get is the provided key element does not match the schema. If I remove the brackets around the attrValue then I get wrong operand type. I know the key exists in the table as i copied and pasted it from there. Also I am succesfully adding things to the table from another function so my connection is working fine. Can anyone please help me out?

推荐答案

您需要在 Key 对象中包含 eventType ,因为您的表架构具有排序键.如果您的表具有排序/分区键,则需要将其与主键一起包括在内.请尝试以下操作:

You need to include the eventType in the Key object because your table schema has a sort key. If your table has a sort/partition key then you need to include it along with the primary key. Try it with the following:

const entrantsParams = {
  'TableName' : 'dev_entrants',
  'Key': {
    'eventID' : 'Qual-919-w5wm1xhnw',
    'eventType' : 'Qual'
  },
  'UpdateExpression' : "SET #attrName = list_append(if_not_exists(#attrName, :empty_list), :attrValue)",
  'ExpressionAttributeNames' : {
    '#attrName' : 'entrants'
  },
  'ExpressionAttributeValues' : {
    ':attrValue' : ['joe'],
    ':empty_list': []
  }
};

这篇关于将字符串添加到dynamodb表中的列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆