Skip to content

Commit 90c58b1

Browse files
authored
Update part3c.md
Added error handling for non-existent notes in the PUT request. If the note doesn't exist, the server now responds with a 400 Bad Request and a relevant error message.
1 parent 5140987 commit 90c58b1

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/content/3/en/part3c.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,8 @@ app.delete('/api/notes/:id', (request, response, next) => {
894894
})
895895
```
896896

897-
In both of the "successful" cases of deleting a resource, the backend responds with the status code <i>204 no content</i>. The two different cases are deleting a note that exists, and deleting a note that does not exist in the database. The _result_ callback parameter could be used for checking if a resource was actually deleted, and we could use that information for returning different status codes for the two cases if we deem it necessary. Any exception that occurs is passed onto the error handler.
897+
In both of the "successful" cases of deleting a resource, the backend responds with the status code <i>204 no content</i>. The two different cases are deleting a note that exists, and deleting a note that does not exist in the database. The _result_ callback parameter could be used for checking if a resource was actually deleted, and we could use that information for returning different status codes for the two cases if we deem it necessary. Similarly, when sending a PUT request with a non-existent ID, it returns null, but the error isn't caught by the catch block, requiring additional handling. Any other exception that occurs is passed onto the error handler.
898+
898899

899900
The toggling of the importance of a note can be easily accomplished with the [findByIdAndUpdate](https://mongoosejs.com/docs/api/model.html#model_Model-findByIdAndUpdate) method.
900901

@@ -909,6 +910,8 @@ app.put('/api/notes/:id', (request, response, next) => {
909910

910911
Note.findByIdAndUpdate(request.params.id, note, { new: true })
911912
.then(updatedNote => {
913+
if(!updatedNote)
914+
return res.status(400).json({ error: 'Note not found' })
912915
response.json(updatedNote)
913916
})
914917
.catch(error => next(error))

0 commit comments

Comments
 (0)