Skip to content

Commit bf6eed6

Browse files
committed
Add explanation why findByIdAndUpdate is not used
1 parent c9230f1 commit bf6eed6

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

src/content/3/en/part3c.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,8 @@ One notable point is that the code now has nested promises, meaning that within
931931
932932
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.
933933
934+
Mongoose also provides the method [findByIdAndUpdate](https://mongoosejs.com/docs/api/model.html#Model.findByIdAndUpdate()), which can be used to find a document by its <i>id</i> and update it with a single method call. However, this approach does not fully suit our needs, because later in this part we define certain requirements for the data stored in the database, and <i>findByIdAndUpdate</i> does not fully support Mongoose's validations. Mongoose's [documentation](https://mongoosejs.com/docs/documents.html#updating-using-queries) also notes that the <i>save()</i> method is generally the correct choice for updating a document, as it provides full validation.
935+
934936
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.
935937
936938
You can find the code for our current application in its entirety in the <i>part3-5</i> branch of [this GitHub repository](https://github.com/fullstack-hy2020/part3-notes-backend/tree/part3-5).

src/content/3/fi/osa3c.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,8 @@ Eräs huomionarvoinen seikka on se, että koodissa on nyt ns. sisäkkäiset prom
920920
921921
Yleensä tällaista ei suositella, koska se voi tehdä koodista vaikealukuista. Tässä tapauksessa ratkaisu kuitenkin toimii, sillä näin voimme varmistua siitä, että _.save()_-metodin jälkeiseen _.then_-lohkoon mennään vain, jos id:tä vastaava muistiinpano on löytynyt kannasta ja _save()_-metodia on kutsuttu. Tutustumme kurssin neljännessä osassa async/await-syntaksiin, joka tarjoaa helpomman ja selkeämmän kirjoitustavan tämänkaltaisiin tilanteisiin.
922922
923+
Mongoose tarjoaa myös metodin [findByIdAndUpdate](https://mongoosejs.com/docs/api/model.html#Model.findByIdAndUpdate()), jonka avulla voi hakea dokumentin <i>id</i>:n perusteella ja päivittää sen yksittäisellä metodikutsulla. Tämä tapa ei kuitenkaan sovellu täysin tarpeisiimme, sillä määrittelemme myöhemmin tässä osassa tietokantaan talletettavalle datalle tiettyjä vaatimuksia, eikä <i>findByIdAndUpdate</i> tue näitä Mongoosen validaatioita täysin. Mongoosen [dokumentaatio](https://mongoosejs.com/docs/documents.html#updating-using-queries) toteaakin, että <i>save()</i>-metodi on lähtökohtaisesti oikea valinta dokumentin päivittämiseen, sillä se tarjoaa täyden validaation.
924+
923925
Backend vaikuttaa toimivan Postmanista ja VS Coden REST Clientistä tehtyjen kokeilujen perusteella. Myös frontend toimii moitteettomasti tietokantaa käyttävän backendin kanssa.
924926
925927
Sovelluksen tämänhetkinen koodi on kokonaisuudessaan [GitHubissa](https://github.com/fullstack-hy2020/part3-notes-backend/tree/part3-5), branchissa <i>part3-5</i>.

0 commit comments

Comments
 (0)