Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

node.js - can't update item in DynamoDB

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.

Here Is my code:

dynamoDB.updateItem({
    "TableName": "mytable",
    "Key": {
        "thing_ID": {"S": "0000"}
    },
    "UpdateExpression": "SET",
    "ExpressionAttributeNames": {
        "SessionID": ""
    },
    "ExpressionAttributeValues": {
        "SessionID": {
            "S": "maybe this works",
        }
    }
})
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...