Skip to content

Commit 15f00b8

Browse files
committed
part 3c
1 parent 54154fa commit 15f00b8

File tree

2 files changed

+220
-189
lines changed

2 files changed

+220
-189
lines changed

src/content/3/en/part3c.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ When the application "does not work", we have to first figure out where the prob
6767

6868
The key is to be systematic. Since the problem can exist anywhere, <i>you must question everything</i>, and eliminate all possibilities one by one. Logging to the console, Postman, debuggers, and experience will help.
6969

70-
When bugs occur, <i>the worst of all possible strategies</i> is to continue writing code. It will guarantee that your code will soon have even more bugs, and debugging them will be even more difficult. The [stop and fix](https://leanscape.io/principles-of-lean-13-jidoka/) principle from Toyota Production Systems is very effective in this situation as well.
70+
When bugs occur, <i>the worst of all possible strategies</i> is to continue writing code. It will guarantee that your code will soon have even more bugs, and debugging them will be even more difficult. The [Jidoka](https://leanscape.io/principles-of-lean-13-jidoka/) (stop and fix) principle from Toyota Production Systems is very effective in this situation as well.
7171

7272
### MongoDB
7373

7474
To store our saved notes indefinitely, we need a database. Most of the courses taught at the University of Helsinki use relational databases. In most parts of this course, we will use [MongoDB](https://www.mongodb.com/) which is a so-called [document database](https://en.wikipedia.org/wiki/Document-oriented_database).
7575

76-
The reason for using Mongo as the database is its lower complexity compared to a relational database. [Part 13](https://fullstackopen.com/en/part13) of the course shows how to build node.js backends that use a relational database.
76+
The reason for using Mongo as the database is its lower complexity compared to a relational database. [Part 13](/en/part13) of the course shows how to build Node.js backends that use a relational database.
7777

7878
Document databases differ from relational databases in how they organize data as well as in the query languages they support. Document databases are usually categorized under the [NoSQL](https://en.wikipedia.org/wiki/NoSQL) umbrella term.
7979

@@ -270,7 +270,7 @@ When the code is executed, the program prints all the notes stored in the databa
270270

271271
![node mongo.js outputs notes as JSON](../../images/3/70new.png)
272272

273-
The objects are retrieved from the database with the [find](https://mongoosejs.com/docs/api/model.html#model_Model-find) method of the _Note_ model. The parameter of the method is an object expressing search conditions. Since the parameter is an empty object<code>{}</code>, we get all of the notes stored in the _notes_ collection.
273+
The objects are retrieved from the database with the [find](https://mongoosejs.com/docs/api/model.html#model_Model-find) method of the _Note_ model. The parameter of the method is an object expressing search conditions. Since the parameter is an empty object<code>{}</code>, we get all of the notes stored in the _notes_ collection.
274274

275275
The search conditions adhere to the Mongo search query [syntax](https://docs.mongodb.com/manual/reference/operator/).
276276

@@ -329,7 +329,7 @@ Arto Vihavainen 045-1232456
329329
Ada Lovelace 040-1231236
330330
</pre>
331331

332-
You can get the command-line parameters from the [process.argv](https://nodejs.org/docs/latest-v8.x/api/process.html#process_process_argv) variable.
332+
You can get the command-line parameters from the [process.argv](https://nodejs.org/docs/latest-v18.x/api/process.html#process_process_argv) variable.
333333

334334
**NB: do not close the connection in the wrong place**. E.g. the following code will not work:
335335

@@ -475,7 +475,7 @@ noteSchema.set('toJSON', {
475475
module.exports = mongoose.model('Note', noteSchema) // highlight-line
476476
```
477477

478-
Defining Node [modules](https://nodejs.org/docs/latest-v8.x/api/modules.html) differs slightly from the way of defining [ES6 modules](/en/part2/rendering_a_collection_modules#refactoring-modules) in part 2.
478+
Defining Node [modules](https://nodejs.org/docs/latest-v18.x/api/modules.html) differs slightly from the way of defining [ES6 modules](/en/part2/rendering_a_collection_modules#refactoring-modules) in part 2.
479479

480480
The public interface of the module is defined by setting a value to the _module.exports_ variable. We will set the value to be the <i>Note</i> model. The other things defined inside of the module, like the variables _mongoose_ and _url_ will not be accessible or visible to users of the module.
481481

@@ -534,7 +534,7 @@ We also added the hardcoded port of the server into the <em>PORT</em> environmen
534534

535535
![.gitignore in vscode with .env line added](../../images/3/45ae.png)
536536

537-
The environment variables defined in the <i>.env</i> file can be taken into use with the expression <em>require('dotenv').config()</em> and you can reference them in your code just like you would reference normal environment variables, with the familiar <em>process.env.MONGODB_URI</em> syntax.
537+
The environment variables defined in the <i>.env</i> file can be taken into use with the expression <em>require('dotenv').config()</em> and you can reference them in your code just like you would reference normal environment variables, with the <em>process.env.MONGODB_URI</em> syntax.
538538

539539
Let's change the <i>index.js</i> file in the following way:
540540

@@ -641,15 +641,15 @@ You can find the code for our current application in its entirety in the <i>part
641641

642642
The following exercises are pretty straightforward, but if your frontend stops working with the backend, then finding and fixing the bugs can be quite interesting.
643643

644-
#### 3.13: Phonebook database, step1
644+
#### 3.13: Phonebook database, step 1
645645

646646
Change the fetching of all phonebook entries so that the data is <i>fetched from the database</i>.
647647

648648
Verify that the frontend works after the changes have been made.
649649

650650
In the following exercises, write all Mongoose-specific code into its own module, just like we did in the chapter [Database configuration into its own module](/en/part3/saving_data_to_mongo_db#database-configuration-into-its-own-module).
651651

652-
#### 3.14: Phonebook database, step2
652+
#### 3.14: Phonebook database, step 2
653653

654654
Change the backend so that new numbers are <i>saved to the database</i>. Verify that your frontend still works after the changes.
655655

@@ -724,9 +724,9 @@ app.get('/api/notes/:id', (request, response) => {
724724
})
725725
```
726726

727-
If the format of the id is incorrect, then we will end up in the error handler defined in the _catch_ block. The appropriate status code for the situation is [400 Bad Request](https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1) because the situation fits the description perfectly:
727+
If the format of the id is incorrect, then we will end up in the error handler defined in the _catch_ block. The appropriate status code for the situation is [400 Bad Request](https://www.rfc-editor.org/rfc/rfc9110.html#name-400-bad-request) because the situation fits the description perfectly:
728728

729-
> <i>The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.</i>
729+
> <i>The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).</i>
730730
731731
We have also added some data to the response to shed some light on the cause of the error.
732732

@@ -741,7 +741,7 @@ It's never a bad idea to print the object that caused the exception to the conso
741741
})
742742
```
743743

744-
The reason the error handler gets called might be something completely different than what you had anticipated. If you log the error to the console, you may save yourself from long and frustrating debugging sessions. Moreover, most modern services where you deploy your application support some form of logging system that you can use to check these logs. As mentioned, Heroku is one.
744+
The reason the error handler gets called might be something completely different than what you had anticipated. If you log the error to the console, you may save yourself from long and frustrating debugging sessions. Moreover, most modern services where you deploy your application support some form of logging system that you can use to check these logs. As mentioned, Fly.io is one.
745745

746746
Every time you're working on a project with a backend, <i>it is critical to keep an eye on the console output of the backend</i>. If you are working on a small screen, it is enough to just see a tiny slice of the output in the background. Any error messages will catch your attention even when the console is far back in the background:
747747

@@ -899,7 +899,7 @@ Notice that the <em>findByIdAndUpdate</em> method receives a regular JavaScript
899899

900900
There is one important detail regarding the use of the <em>findByIdAndUpdate</em> method. By default, the <em>updatedNote</em> parameter of the event handler receives the original document [without the modifications](https://mongoosejs.com/docs/api/model.html#model_Model-findByIdAndUpdate). We added the optional <code>{ new: true }</code> parameter, which will cause our event handler to be called with the new modified document instead of the original.
901901

902-
After testing the backend directly with Postman and 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.
902+
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.
903903

904904
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).
905905

@@ -927,25 +927,25 @@ Full stack development is <i> extremely hard</i>, that is why I will use all the
927927

928928
### Exercises 3.15.-3.18.
929929

930-
#### 3.15: Phonebook database, step3
930+
#### 3.15: Phonebook database, step 3
931931

932932
Change the backend so that deleting phonebook entries is reflected in the database.
933933

934934
Verify that the frontend still works after making the changes.
935935

936-
#### 3.16: Phonebook database, step4
936+
#### 3.16: Phonebook database, step 4
937937

938938
Move the error handling of the application to a new error handler middleware.
939939

940-
#### 3.17*: Phonebook database, step5
940+
#### 3.17*: Phonebook database, step 5
941941

942942
If the user tries to create a new phonebook entry for a person whose name is already in the phonebook, the frontend will try to update the phone number of the existing entry by making an HTTP PUT request to the entry's unique URL.
943943

944944
Modify the backend to support this request.
945945

946946
Verify that the frontend works after making your changes.
947947

948-
#### 3.18*: Phonebook database step6
948+
#### 3.18*: Phonebook database step 6
949949

950950
Also update the handling of the <i>api/persons/:id</i> and <i>info</i> routes to use the database, and verify that they work directly with the browser, Postman, or VS Code REST client.
951951

0 commit comments

Comments
 (0)