can't update item in DynamoDB

前端 未结 3 1716
旧时难觅i
旧时难觅i 2020-12-09 12:58

I have been trying to figure out how to update an item in dynamoDB but have not had any success.

I know how to add and item and remove an item but not update.

3条回答
  •  执念已碎
    2020-12-09 13:24

    It looks like you are trying to update an item by using an Expression, and in this case, your UpdateExpression is incorrect. Both the ExpressionAttributeNames and ExpressionAttributeValues are used for placeholder substitution in your expression.

    I think your code would look something like this, if you want to set an attribute for an item:

    dynamoDB.updateItem({  
        "TableName" : "exampleTable",
        "Key" : {
            "hashAttributeName" : {
                "S" : "thing_ID"
            }
        },
        "UpdateExpression" : "SET #attrName =:attrValue",
        "ExpressionAttributeNames" : {
            "#attrName" : "SessionID"
        },
        "ExpressionAttributeValues" : {
            ":attrValue" : {
                "S" : "maybe this works"
            }
        }
    });
    

    This will update an item that looks like this:

    {  
        "Item":{  
            "hashAttributeName":"thing_ID"
        }
    }
    

    To this:

    {  
        "Item":{  
            "hashAttributeName" : "thing_ID",
            "SessionID" : "maybe this works"
        }
    }
    

提交回复
热议问题