Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...