1
votes

I am using a dynamodb database and attempting to update a value via node.js. I have created a table called counter in order to determine the current count of students stored in a separate table. I have had luck updating string values, but have had nothing but trouble trying to update this counter value. I have tried many different configurations of code over the last several hours to no avail. Any help would be appreciated.

function updateCount(operand) {
var params = {
    TableName : "counter",
    Key:{
        "type" : {
            "S" : "student"
        }
    },
    UpdateExpression : "SET pos = pos + :o", 
    ExpressionAttributeValues:{
        ":o": Number(operand)   
    },
    ReturnValues:"UPDATED_NEW"
}
docClient.update(params, function(err, data) {
if (err)
        console.log(err); 
    else {
        console.log(data); 
        res.send(JSON.stringify(data, null, 2));
    }
});

} updateCount(1);

The current error shows this but I've had many more: C:\Users\Ko_Kor\Desktop\CS 496\assignment#3>node server.js Express started on http://localhost:8081; press Ctrl-C to terminate. { [ValidationException: The provided key element does not match the schema] message: 'The provided key element does not match the schema', code: 'ValidationException', time: Fri Oct 21 2016 20:36:43 GMT-0700 (Pacific Daylight Time), requestId: 'T8G9V8KF587THVK5OTGQ95TH9VVV4KQNSO5AEMVJF66Q9ASUAAJG', statusCode: 400, retryable: false, retryDelay: 0 }

I have also tried using this format:

ExpressionAttributeValues:{
    ":o": {"N": operand}    
},
2

2 Answers

0
votes

DynamoDB doesn't provide this kind of feature. You can try to play with DynamoDB atomic counters, but I will suggest to try another approach like string UUID.

0
votes

You could use ADD

[...]
UpdateExpression: "ADD counter :o SET updatedAt = :updatedAt",
ExpressionAttributeValues: {
  ':o': valueToIncrementWith,
  ':updatedAt': Date.now()
}
[...]