title: Unable to increment score in dynamodb table using DynamoDB document client - Javascript
body: <p>I have a working serverless application deployed on Lambda (nodejs6.10) and can create and read users from my DynamoDB, however, I have been having problems trying to perform an update on a specific attribute.</p>
  
  <p>Basically, my table has a key of userId and two attributes called email and score.</p>
  
  <p>The application detects if a referral code (userId) was supplied and if so it should increment their score by 1. Below are the params that I am passing to the dynamoDb.update function.</p>
  
  <pre><code>if (refcode) {
        console.log("A referral code: " + refcode + " was detected");
  
        const params = {
          TableName: USERS_TABLE,
          Key: {
            userId: refcode
          },
          UpdateExpression: "set score = score + :val",
          ExpressionAttributeValues: {
            ":val": 1
          },
          ReturnValues: "UPDATED_NEW"
        };
  
        console.log(params);
  
        dynamoDb.update(params, (error, result) =&gt; {
          console.log("Checking for error...");
          if (error) {
            console.log(error);
            res.status(400), json({ error: "Could not GET user" });
          }
          console.log("Checking for result...");
          if (result.Item) {
            console.log("Item updated");
            const { userId, email, score } = result.Item;
          } else {
            res.status(404).json({ error: "Invalid referral code" });
            console.log("Invalid ref code");
          }
        });
      }
  </code></pre>
  
  <p>In Cloudwatch I can see that my function has entered this part of the logic successfully, however, it looks like it never runs the dynamoDb.update part. Here are the cloudwatch logs:</p>
  
  <pre><code>START RequestId: 7d92d4da-a710-11e8-abdd-039e23e278bd Version: $LATEST
  2018-08-23T20:09:52.392Z    7d92d4da-a710-11e8-abdd-039e23e278bd    A referral code: cEBeGM1sk was detected
  2018-08-23T20:09:52.393Z    7d92d4da-a710-11e8-abdd-039e23e278bd    { TableName: '**&lt;redacted&gt;**',
  Key: { userId: 'cEBeGM1sk' },
  UpdateExpression: 'set score = score + :val',
  ExpressionAttributeValues: { ':val': 1 },
  ReturnValues: 'UPDATED_NEW' }
  2018-08-23T20:09:52.550Z    7d92d4da-a710-11e8-abdd-039e23e278bd    Reached the end - taking user to thank you page
  END RequestId: 7d92d4da-a710-11e8-abdd-039e23e278bd
  REPORT RequestId: 7d92d4da-a710-11e8-abdd-039e23e278bd  Duration: 1530.76 ms    Billed Duration: 1600 ms Memory Size: 128 MB    Max Memory Used: 45 MB  
  </code></pre>
  
  <p>Any help much appreciated! It should work according to the atomic update example given on the AWS documentation: <a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.NodeJs.03.html#GettingStarted.NodeJs.03.03" rel="nofollow noreferrer">AWS Documentation</a></p>