Skip to content

Commit f83c691

Browse files
committed
Merge branch 'test-delete-assert-by-id' of https://github.com/AbelGAlem/fullstack-hy2020.github.io into test-delete-assert-by-id
2 parents 86407ba + d2b2132 commit f83c691

File tree

1 file changed

+16
-38
lines changed

1 file changed

+16
-38
lines changed

src/content/3/en/part3c.md

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -887,49 +887,27 @@ In both of the "successful" cases of deleting a resource, the backend responds w
887887
Let's implement the functionality to update a single note, allowing the importance of the note to be changed. The note updating is done as follows:
888888

889889
```js
890-
app.put('/api/notes/:id', (request, response, next) => {
891-
const { content, important } = request.body
892-
893-
Note.findById(request.params.id)
894-
.then(note => {
895-
if (!note) {
896-
return response.status(404).end()
897-
}
898-
899-
note.content = content
900-
note.important = important
901-
902-
return note.save().then((updatedNote) => {
903-
response.json(updatedNote)
904-
})
905-
})
906-
.catch(error => next(error))
890+
app.put("/api/notes/:id", (request, response, next) => {
891+
const {content, important} = request.body
892+
893+
Person.findByIdAndUpdate(
894+
request.params.id,
895+
{note: content, important: important},
896+
{new: true}
897+
).then(updatedNote => {
898+
if(!updatedNote){
899+
return response.status(404).end()
900+
}
901+
return response.json(updatedNote);
902+
}).catch(error => next(error))
907903
})
908904
```
909905

910-
The note to be updated is first fetched from the database using the _findById_ method. If no object is found in the database with the given id, the value of the variable _note_ is _null_, and the query responds with the status code <i>404 Not Found</i>.
911-
912-
If an object with the given id is found, its _content_ and _important_ fields are updated with the data provided in the request, and the modified note is saved to the database using the _save()_ method. The HTTP request responds by sending the updated note in the response.
906+
The note to be updated is modified directly in the database using the findByIdAndUpdate method. If no object is found in the database with the given id, the value of the variable updatedNote is null, and the query responds with the status code <i>404 Not Found</i>.
913907

914-
One notable point is that the code now has nested promises, meaning that within the outer _.then_ method, another [promise chain](https://javascript.info/promise-chaining) is defined:
915-
916-
```js
917-
.then(note => {
918-
if (!note) {
919-
return response.status(404).end()
920-
}
921-
922-
note.content = content
923-
note.important = important
924-
925-
// highlight-start
926-
return note.save().then((updatedNote) => {
927-
response.json(updatedNote)
928-
})
929-
// highlight-end
930-
```
908+
If an object with the given id is found, its content and important fields are updated with the data provided in the request, and the database returns the updated note. The HTTP request responds by sending this updated note in the response.
931909

932-
Usually, this is not recommended because it can make the code difficult to read. In this case, however, the solution works because it ensures that the _.then_ block following the _save()_ method is only executed if a note with the given id is found in the database and the _save()_ method is called. In the fourth part of the course, we will explore the async/await syntax, which offers an easier and clearer way to handle such situations.
910+
In the options given to findByIdAndUpdate, the value of the <i>new</i> field is set to <i>true</i>. This tells Mongoose to return the updated document instead of the original version from before the update.
933911

934912
After testing the backend directly with Postman or the VS Code REST client, we can verify that it seems to work. The frontend also appears to work with the backend using the database.
935913

0 commit comments

Comments
 (0)