# Add an Update Note API

**URL:** <https://discourse.sst.dev/t/add-an-update-note-api/144>\
**Category:** Chapter Comments\
**Created:** [May 9, 2018, 8:09pm UTC](https://discourse.sst.dev/t/add-an-update-note-api/144 "2018-05-09T20:09:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:09pm UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/1 "2018-05-09T20:09:42Z")

</div>

_From @jayair on Mon Apr 10 2017 01:00:17 GMT+0000 (UTC)_  
  
Link to chapter - [http://serverless-stack.com/chapters/add-an-update-note-api.html](http://serverless-stack.com/chapters/add-an-update-note-api.html)  
  
_Copied from original issue: [https://github.com/AnomalyInnovations/serverless-stack-com/issues/26](https://github.com/AnomalyInnovations/serverless-stack-com/issues/26)_

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:09pm UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/2 "2018-05-09T20:09:42Z")

</div>

_From @oluckyman on Sun Apr 16 2017 00:22:39 GMT+0000 (UTC)_  
  
why use  
`ReturnValues: 'ALL_NEW',`  
if the response returns `success: true` only?

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:09pm UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/3 "2018-05-09T20:09:43Z")

</div>

_From @fwang on Sun Apr 16 2017 16:48:52 GMT+0000 (UTC)_  
  
You are totally right. We are not using the results any where in the code. It is in there in case users want to inspect the return value just for learning purposes.

Feel free to set ReturnValues to ‘NONE’ or remove the line.

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:09pm UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/4 "2018-05-09T20:09:43Z")

</div>

_From @oluckyman on Sun Apr 16 2017 19:00:41 GMT+0000 (UTC)_  
  
cool, thanks!  
and thanks for the content, great material and easy to read. I like how you  
balance explaining things and avoiding rabbit holes, without overwhelming  
with too much information

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:09pm UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/5 "2018-05-09T20:09:43Z")

</div>

_From @walshe on Wed Jan 17 2018 03:07:39 GMT+0000 (UTC)_  
  
wouldn’t it be a good idea to use a ‘version’ field for an optimistic locking approach ?

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:09pm UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/6 "2018-05-09T20:09:43Z")

</div>

_From @jayair on Fri Jan 19 2018 01:13:37 GMT+0000 (UTC)_  
  
@walshe Do you mean a version to keep track of the edits made to a note? You can definitely do that! We are just keeping it fairly simple in the tutorial.

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:09pm UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/7 "2018-05-09T20:09:44Z")

</div>

_From @walshe on Fri Jan 19 2018 01:29:01 GMT+0000 (UTC)_  
  
@jayair not exactly, I am talking about handling concurrent updates, so that if an entity has changed between the time it takes someone to read and update the entity then the update would fail similar to an ‘update x set a=, b=,c= where version=’

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:09pm UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/8 "2018-05-09T20:09:44Z")

</div>

_From @jayair on Fri Jan 19 2018 22:29:37 GMT+0000 (UTC)_  
  
@walshe Oh yeah I see what you are saying. That is definitely a good idea. I think we could cover it in an advanced chapter maybe?

For anybody else reading this; if two users are editing the same note, one could overwrite the other since we simply update the note with the passed in content. By adding a `version` field we can ensure that the note we are editing is the current version of it (as opposed to one that has already been updated in Dynamo).

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:09pm UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/9 "2018-05-09T20:09:44Z")

</div>

_From @walshe on Sat Jan 20 2018 02:57:47 GMT+0000 (UTC)_  
  
yeah I think you just need a condition expression in the update

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:09pm UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/10 "2018-05-09T20:09:45Z")

</div>

_From @allasandeep on Sat Mar 24 2018 23:26:54 GMT+0000 (UTC)_  
  
I am getting this error, can anyone help me?

```auto
serverless invoke local --function update --path mocks/update-event.json

  Y A M L Exception --------------------------------------

  duplicated mapping key in "C:\Users\gayat\Desktop\project-app-api\serverless.yml" at line 75, column 5:
        handler: update.main
        ^

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Get Support --------------------------------------------
     Docs: docs.serverless.com
     Bugs: github.com/serverless/serverless/issues
     Forums: forum.serverless.com
     Chat: gitter.im/serverless/serverless

  Your Environment Information -----------------------------
     OS: win32
     Node Version: 8.10.0
     Serverless Version: 1.26.1

```

---

<div class="post-metadata">

**Author:** ![karthickng](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@karthickng](https://discourse.sst.dev/u/karthickng)\
**Post date:** [May 24, 2018, 10:50am UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/11 "2018-05-24T10:50:22Z")

</div>

Shouldn’t the Update Note API use POST instead of PUT. PUT creates a new note if the ID is not present already.

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [May 24, 2018, 7:13pm UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/12 "2018-05-24T19:13:30Z")

</div>

Not necessarily but there isn’t a super strict rule here. You could use PUT and POST to create if you wanted to. The rule we typically follow is to use PUT for idempotent actions.

---

<div class="post-metadata">

**Author:** ![ReedME](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@ReedME](https://discourse.sst.dev/u/ReedME)\
**Post date:** [May 25, 2018, 9:07am UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/13 "2018-05-25T09:07:38Z")

</div>

I have an issue where my code is just creating new entries instead of updating an existing entry. All code has been copied from the tutorial pages, however I have replaced the wording ‘notes’ with ‘dockets’ and ‘noteId’ with ‘docketId’

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [May 25, 2018, 5:52pm UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/14 "2018-05-25T17:52:04Z")

</div>

Are you trying to update the `docketId`? Usually it creates new entires if you are updating the primary key.

---

<div class="post-metadata">

**Author:** ![ReedME](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@ReedME](https://discourse.sst.dev/u/ReedME)\
**Post date:** [May 25, 2018, 10:11pm UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/15 "2018-05-25T22:11:27Z")

</div>

I discovered my problem, when I was invoking the serverless command I wasn’t changing the function I was using, woops!

---

<div class="post-metadata">

**Author:** ![artur79](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/artur79/32/470_2.png) [@artur79](https://discourse.sst.dev/u/artur79)\
**Post date:** [May 6, 2019, 10:18am UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/16 "2019-05-06T10:18:42Z")

</div>

Not sure is is a bug or just DynamoDB feature, but when you use some random non-existing id in _pathParameters/id_ a record will be created anyway. Although its attribute _createdAt_ will be empty.

---

<div class="post-metadata">

**Author:** ![boxabirds](https://avatars.discourse-cdn.com/v4/letter/b/c57346/32.png) [@boxabirds](https://discourse.sst.dev/u/boxabirds)\
**Post date:** [December 21, 2019, 12:39am UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/17 "2019-12-21T00:39:25Z")

</div>

Hi y’all I’m getting internal server error when trying to update an item (error 500). I’ve tried looking around but can’t figure out what system will actually tell me what the problem is, where, and ideally what to do about it.

```auto
export async function main (event, context) {
  const data = JSON.parse(event.body)
  const params = {
    TableName: process.env.tableName,
    Key: {
      userId: {"S": event.requestContext.identity.cognitoIdentityId},
      nodeId: {"S": event.pathParameters.id}
    },
    UpdateExpression: "SET content = :content, attachment = :attachment",
    ExpressionAttributeValues: {
      ":attachment": {"S":data.attachment || null},
      ":content": {"S": data.content || null}
    },
    ReturnValues: "ALL_NEW"
  }
  try {
    await dynamoDbLib.call('update', params)
    return (success({ status:true }))
  } catch (e) {
    return failure({ status: false, params:JSON.stringify(params) })
  }
}

```

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [January 12, 2020, 3:15am UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/18 "2020-01-12T03:15:24Z")

</div>

Make sure to `console.log(e)` before we the `return failure` line.

---

<div class="post-metadata">

**Author:** ![biganth](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/biganth/32/174_2.png) [@biganth](https://discourse.sst.dev/u/biganth)\
**Post date:** [July 13, 2020, 8:51pm UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/19 "2020-07-13T20:51:05Z")

</div>

I was concerned about this in my app too. You can add a ConditionExpression to your params, either of the below should work. See [https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ConditionExpressions.html#Expressions.ConditionExpressions.PreventingOverwrites](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ConditionExpressions.html#Expressions.ConditionExpressions.PreventingOverwrites)

```auto
ConditionExpression: 'attribute_exists(plantId)'

```

OR

```auto
ConditionExpression: 'plantId = :plantIdVal'

```

---

<div class="post-metadata">

**Author:** ![will](https://avatars.discourse-cdn.com/v4/letter/w/90ced4/32.png) [@will](https://discourse.sst.dev/u/will)\
**Post date:** [September 4, 2020, 1:47am UTC](https://discourse.sst.dev/t/add-an-update-note-api/144/20 "2020-09-04T01:47:12Z")

</div>

You can add the following entry to your params object:

```auto
ConditionExpression: 'attribute_exists(noteId)',

```

However the HTTP response will have an odd error message:

```auto
{
    "statusCode": 500,
    "body": "{\"error\":\"The conditional request failed\"}",
    "headers": {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Credentials": true
    }
}

```

[Next page](https://discourse.sst.dev/t/add-an-update-note-api/144.md?page=2)
