You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/2/en/part2c.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,25 +17,25 @@ Create a file named <i>db.json</i> in the root directory of the previous <i>note
17
17
{
18
18
"notes": [
19
19
{
20
-
"id": 1,
20
+
"id": "1",
21
21
"content": "HTML is easy",
22
22
"important": true
23
23
},
24
24
{
25
-
"id": 2,
25
+
"id": "2",
26
26
"content": "Browser can execute only JavaScript",
27
27
"important": false
28
28
},
29
29
{
30
-
"id": 3,
30
+
"id": "3",
31
31
"content": "GET and POST are the most important methods of HTTP protocol",
32
32
"important": true
33
33
}
34
34
]
35
35
}
36
36
```
37
37
38
-
You can [install](https://github.com/typicode/json-server#getting-started) a JSON server globally on your machine using the command _npm install -g json-server_. A global installation requires administrative privileges, which means that it is not possible on faculty computers or freshman laptops.
38
+
You can [install](https://github.com/typicode/json-server#getting-started) a JSON server globally on your machine using the command _npm install -g json-server_. A global installation requires administrative privileges.
39
39
40
40
After installing run the following command to run the json-server. The <i>json-server</i> starts running on port 3000 by default; we will now define an alternate port 3001, for the json-server. The --watch option automatically looks for any saved changes to db.json
41
41
@@ -541,22 +541,22 @@ We continue with developing the phonebook. Store the initial state of the applic
Copy file name to clipboardExpand all lines: src/content/2/fi/osa2c.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,25 +17,25 @@ Tehdään projektin juurihakemistoon tiedosto <i>db.json</i>:
17
17
{
18
18
"notes": [
19
19
{
20
-
"id": 1,
20
+
"id": "1",
21
21
"content": "HTML is easy",
22
22
"important": true
23
23
},
24
24
{
25
-
"id": 2,
25
+
"id": "2",
26
26
"content": "Browser can execute only JavaScript",
27
27
"important": false
28
28
},
29
29
{
30
-
"id": 3,
30
+
"id": "3",
31
31
"content": "GET and POST are the most important methods of HTTP protocol",
32
32
"important": true
33
33
}
34
34
]
35
35
}
36
36
```
37
37
38
-
JSON Server on mahdollista [asentaa](https://github.com/typicode/json-server#install) koneelle ns. globaalisti komennolla _npm install -g json-server_. Globaali asennus edellyttää kuitenkin pääkäyttäjän oikeuksia eli se ei ole mahdollista laitoksen koneilla tai uusilla fuksiläppäreillä.
38
+
JSON Server on mahdollista [asentaa](https://github.com/typicode/json-server#install) koneelle ns. globaalisti komennolla _npm install -g json-server_. Globaali asennus edellyttää kuitenkin pääkäyttäjän oikeuksia.
39
39
40
40
Globaali asennus ei ole kuitenkaan tarpeen, sillä voimme käynnistää JSON Serverin myös _npx_-komennon avulla:
Copy file name to clipboardExpand all lines: src/content/3/en/part3a.md
+17-70Lines changed: 17 additions & 70 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -484,61 +484,8 @@ const id = request.params.id
484
484
485
485
The now familiar _find_ method of arrays is used to find the note with an id that matches the parameter. The note is then returned to the sender of the request.
486
486
487
-
When we test our application by going to <http://localhost:3001/api/notes/1> in our browser, we notice that it does not appear to work, as the browser displays an empty page. This comes as no surprise to us as software developers, and it's time to debug.
487
+
We can now test our application by going to <http://localhost:3001/api/notes/1> in our browser:
488
488
489
-
Adding _console.log_ commands into our code is a time-proven trick:
When we visit <http://localhost:3001/api/notes/1> again in the browser, the console - which is the terminal (in this case) - will display the following:
502
-
503
-

504
-
505
-
The id parameter from the route is passed to our application but the _find_ method does not find a matching note.
506
-
507
-
To further our investigation, we also add a console log inside the comparison function passed to the _find_ method. To do this, we have to get rid of the compact arrow function syntax <em>note => note.id === id</em>, and use the syntax with an explicit return statement:
When we visit the URL again in the browser, each call to the comparison function prints a few different things to the console. The console output is the following:
522
-
523
-
<pre>
524
-
1 'number' '1' 'string' false
525
-
2 'number' '1' 'string' false
526
-
3 'number' '1' 'string' false
527
-
</pre>
528
-
529
-
The cause of the bug becomes clear. The _id_ variable contains a string '1', whereas the ids of notes are integers. In JavaScript, the "triple equals" comparison === considers all values of different types to not be equal by default, meaning that 1 is not '1'.
530
-
531
-
Let's fix the issue by changing the id parameter from a string into a [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number):
@@ -727,28 +674,28 @@ Let's return to the application. Once we know that the application receives data
727
674
```js
728
675
app.post('/api/notes', (request, response) => {
729
676
constmaxId=notes.length>0
730
-
?Math.max(...notes.map(n=>n.id))
677
+
?Math.max(...notes.map(n=>Number(n.id)))
731
678
:0
732
679
733
680
constnote=request.body
734
-
note.id= maxId +1
681
+
note.id=String(maxId +1)
735
682
736
683
notes =notes.concat(note)
737
684
738
685
response.json(note)
739
686
})
740
687
```
741
688
742
-
We need a unique id for the note. First, we find out the largest id number in the current list and assign it to the _maxId_ variable. The id of the new note is then defined as _maxId + 1_. This method is not recommended, but we will live with it for now as we will replace it soon enough.
689
+
We need a unique id for the note. First, we find out the largest id number in the current list and assign it to the _maxId_ variable. The id of the new note is then defined as _maxId + 1_ as a string. This method is not recommended, but we will live with it for now as we will replace it soon enough.
743
690
744
-
The current version still has the problem that the HTTP POST request can be used to add objects with arbitrary properties. Let's improve the application by defining that the <i>content</i> property may not be empty. The <i>important</i> property will be given default value false. All other properties are discarded:
691
+
The current version still has the problem that the HTTP POST request can be used to add objects with arbitrary properties. Let's improve the application by defining that the <i>content</i> property may not be empty. The <i>important</i> property will be given a default value of false. All other properties are discarded:
745
692
746
693
```js
747
694
constgenerateId= () => {
748
695
constmaxId=notes.length>0
749
-
?Math.max(...notes.map(n=>n.id))
696
+
?Math.max(...notes.map(n=>Number(n.id)))
750
697
:0
751
-
return maxId +1
698
+
returnString(maxId +1)
752
699
}
753
700
754
701
app.post('/api/notes', (request, response) => {
@@ -808,19 +755,19 @@ One more thing before we move on to the exercises. The function for generating I
808
755
```js
809
756
constgenerateId= () => {
810
757
constmaxId=notes.length>0
811
-
?Math.max(...notes.map(n=>n.id))
758
+
?Math.max(...notes.map(n=>Number(n.id)))
812
759
:0
813
-
return maxId +1
760
+
returnString(maxId +1)
814
761
}
815
762
```
816
763
817
764
The function body contains a row that looks a bit intriguing:
818
765
819
766
```js
820
-
Math.max(...notes.map(n=>n.id))
767
+
Math.max(...notes.map(n=>Number(n.id)))
821
768
```
822
769
823
-
What exactly is happening in that line of code? <em>notes.map(n => n.id)</em> creates a new array that contains all the ids of the notes. [Math.max](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max) returns the maximum value of the numbers that are passed to it. However, <em>notes.map(n => n.id)</em> is an <i>array</i> so it can't directly be given as a parameter to _Math.max_. The array can be transformed into individual numbers by using the "three dot" [spread](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) syntax <em>...</em>.
770
+
What exactly is happening in that line of code? <em>notes.map(n => n.id)</em> creates a new array that contains all the ids of the notes in number form. [Math.max](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max) returns the maximum value of the numbers that are passed to it. However, <em>notes.map(n => Number(n.id))</em> is an <i>array</i> so it can't directly be given as a parameter to _Math.max_. The array can be transformed into individual numbers by using the "three dot" [spread](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) syntax <em>...</em>.
0 commit comments