简体   繁体   English

updateItem/putItem object 到 dynamodb 中的数组

[英]updateItem/putItem object into array in dynamodb

I'm having trouble pushing an object into an array/Set the object into it as docs state in dynamodb.我无法将 object 推入数组/将 object 设置为 dynamodb 中的文档 state。 Getting the error:得到错误:

Invalid UpdateExpression: Incorrect number of operands for operator or function; operator or function: if_not_exists, number of operands: 1

What am I doing wrong?我究竟做错了什么? I appreciate any help!感谢您的帮助!

   export async function postTrade(position) {
    let params = {
        Key: {
            PK: 'stocks' 
        },
        UpdateExpression: 'set #tickers = list_append(if_not_exists(#tickers))',
        ExpressionAttributeNames: {
            '#tickers': 'tickers'
        },
        ExpressionAttributeValues: {
            ':tickers': [position]
        },
        TableName: 'stockTable'
    };
    await docClient.update(params).promise();
}

Dynamo record发电机记录

{
 "PK": "stocks",
 "tickers": [
 ]
}

Appending to a list附加到列表

list_append takes 2 parameters, not one: list_append需要 2 个参数,而不是一个:

  • list_append (list1, list2)

  • The function takes two lists as input and appends all elements from list2 to list1 function 以两个列表作为输入并将所有元素从list2附加到list1

Checking an attribute exists检查属性是否存在

Likewise, if_not_exists() takes 2 parameters:同样, if_not_exists()有两个参数:

  • if_not_exists (path, value)

  • If the item does not contain an attribute at the specified path, if_not_exists evaluates to value ;如果项目在指定路径不包含属性,则if_not_exists的计算结果为value otherwise, it evaluates to path .否则,它的计算结果为path Therefore, you should use an empty array as a parameter.因此,您应该使用一个空数组作为参数。

Solution解决方案

Pass 2 parmeters to each function, which should allow you to append to a list.将 2 个参数传递给每个 function,这应该允许您将 append 加入列表。

    let params = {
        Key: {
            PK: 'stocks' 
        },
        UpdateExpression: 'set #tickers = list_append(if_not_exists(#tickers, :empty_list), :tickers)',
        ExpressionAttributeNames: {
            '#tickers': 'tickers'
        },
        ExpressionAttributeValues: {
            ':tickers': [position],
            ':empty_list': []
        },
        TableName: 'stockTable'
    };

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM