Skip to content

Commit 055ff3f

Browse files
committed
ids to strings
1 parent 3c9ee65 commit 055ff3f

File tree

4 files changed

+48
-155
lines changed

4 files changed

+48
-155
lines changed

src/content/2/en/part2c.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@ Create a file named <i>db.json</i> in the root directory of the previous <i>note
1717
{
1818
"notes": [
1919
{
20-
"id": 1,
20+
"id": "1",
2121
"content": "HTML is easy",
2222
"important": true
2323
},
2424
{
25-
"id": 2,
25+
"id": "2",
2626
"content": "Browser can execute only JavaScript",
2727
"important": false
2828
},
2929
{
30-
"id": 3,
30+
"id": "3",
3131
"content": "GET and POST are the most important methods of HTTP protocol",
3232
"important": true
3333
}
3434
]
3535
}
3636
```
3737

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.
3939

4040
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
4141

@@ -541,22 +541,22 @@ We continue with developing the phonebook. Store the initial state of the applic
541541
{
542542
"name": "Arto Hellas",
543543
"number": "040-123456",
544-
"id": 1
544+
"id": "1"
545545
},
546546
{
547547
"name": "Ada Lovelace",
548548
"number": "39-44-5323523",
549-
"id": 2
549+
"id": "2"
550550
},
551551
{
552552
"name": "Dan Abramov",
553553
"number": "12-43-234345",
554-
"id": 3
554+
"id": "3"
555555
},
556556
{
557557
"name": "Mary Poppendieck",
558558
"number": "39-23-6423122",
559-
"id": 4
559+
"id": "4"
560560
}
561561
]
562562
}

src/content/2/fi/osa2c.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@ Tehdään projektin juurihakemistoon tiedosto <i>db.json</i>:
1717
{
1818
"notes": [
1919
{
20-
"id": 1,
20+
"id": "1",
2121
"content": "HTML is easy",
2222
"important": true
2323
},
2424
{
25-
"id": 2,
25+
"id": "2",
2626
"content": "Browser can execute only JavaScript",
2727
"important": false
2828
},
2929
{
30-
"id": 3,
30+
"id": "3",
3131
"content": "GET and POST are the most important methods of HTTP protocol",
3232
"important": true
3333
}
3434
]
3535
}
3636
```
3737

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.
3939

4040
Globaali asennus ei ole kuitenkaan tarpeen, sillä voimme käynnistää JSON Serverin myös _npx_-komennon avulla:
4141

@@ -537,22 +537,22 @@ Jatketaan puhelinluettelon kehittämistä. Talleta sovelluksen alkutila projekti
537537
{
538538
"name": "Arto Hellas",
539539
"number": "040-123456",
540-
"id": 1
540+
"id": "1"
541541
},
542542
{
543543
"name": "Ada Lovelace",
544544
"number": "39-44-5323523",
545-
"id": 2
545+
"id": "2"
546546
},
547547
{
548548
"name": "Dan Abramov",
549549
"number": "12-43-234345",
550-
"id": 3
550+
"id": "3"
551551
},
552552
{
553553
"name": "Mary Poppendieck",
554554
"number": "39-23-6423122",
555-
"id": 4
555+
"id": "4"
556556
}
557557
]
558558
}

src/content/3/en/part3a.md

Lines changed: 17 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -484,61 +484,8 @@ const id = request.params.id
484484

485485
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.
486486

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:
488488

489-
Adding _console.log_ commands into our code is a time-proven trick:
490-
491-
```js
492-
app.get('/api/notes/:id', (request, response) => {
493-
const id = request.params.id
494-
console.log(id)
495-
const note = notes.find(note => note.id === id)
496-
console.log(note)
497-
response.json(note)
498-
})
499-
```
500-
501-
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-
![terminal displaying 1 then undefined](../../images/3/8.png)
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:
508-
509-
```js
510-
app.get('/api/notes/:id', (request, response) => {
511-
const id = request.params.id
512-
const note = notes.find(note => {
513-
console.log(note.id, typeof note.id, id, typeof id, note.id === id)
514-
return note.id === id
515-
})
516-
console.log(note)
517-
response.json(note)
518-
})
519-
```
520-
521-
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):
532-
533-
```js
534-
app.get('/api/notes/:id', (request, response) => {
535-
const id = Number(request.params.id) // highlight-line
536-
const note = notes.find(note => note.id === id)
537-
response.json(note)
538-
})
539-
```
540-
541-
Now fetching an individual resource works.
542489

543490
![api/notes/1 gives a single note as JSON](../../images/3/9new.png)
544491

@@ -556,7 +503,7 @@ Let's make the following change to our code:
556503

557504
```js
558505
app.get('/api/notes/:id', (request, response) => {
559-
const id = Number(request.params.id)
506+
const id = request.params.id
560507
const note = notes.find(note => note.id === id)
561508

562509
// highlight-start
@@ -583,7 +530,7 @@ Next, let's implement a route for deleting resources. Deletion happens by making
583530

584531
```js
585532
app.delete('/api/notes/:id', (request, response) => {
586-
const id = Number(request.params.id)
533+
const id = request.params.id
587534
notes = notes.filter(note => note.id !== id)
588535

589536
response.status(204).end()
@@ -727,28 +674,28 @@ Let's return to the application. Once we know that the application receives data
727674
```js
728675
app.post('/api/notes', (request, response) => {
729676
const maxId = notes.length > 0
730-
? Math.max(...notes.map(n => n.id))
677+
? Math.max(...notes.map(n => Number(n.id)))
731678
: 0
732679

733680
const note = request.body
734-
note.id = maxId + 1
681+
note.id = String(maxId + 1)
735682

736683
notes = notes.concat(note)
737684

738685
response.json(note)
739686
})
740687
```
741688

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.
743690

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:
745692

746693
```js
747694
const generateId = () => {
748695
const maxId = notes.length > 0
749-
? Math.max(...notes.map(n => n.id))
696+
? Math.max(...notes.map(n => Number(n.id)))
750697
: 0
751-
return maxId + 1
698+
return String(maxId + 1)
752699
}
753700

754701
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
808755
```js
809756
const generateId = () => {
810757
const maxId = notes.length > 0
811-
? Math.max(...notes.map(n => n.id))
758+
? Math.max(...notes.map(n => Number(n.id)))
812759
: 0
813-
return maxId + 1
760+
return String(maxId + 1)
814761
}
815762
```
816763

817764
The function body contains a row that looks a bit intriguing:
818765

819766
```js
820-
Math.max(...notes.map(n => n.id))
767+
Math.max(...notes.map(n => Number(n.id)))
821768
```
822769

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>.
824771

825772
</div>
826773

@@ -843,22 +790,22 @@ Data:
843790
```js
844791
[
845792
{
846-
"id": 1,
793+
"id": "1",
847794
"name": "Arto Hellas",
848795
"number": "040-123456"
849796
},
850797
{
851-
"id": 2,
798+
"id": "2",
852799
"name": "Ada Lovelace",
853800
"number": "39-44-5323523"
854801
},
855802
{
856-
"id": 3,
803+
"id": "3",
857804
"name": "Dan Abramov",
858805
"number": "12-43-234345"
859806
},
860807
{
861-
"id": 4,
808+
"id": "4",
862809
"name": "Mary Poppendieck",
863810
"number": "39-23-6423122"
864811
}

0 commit comments

Comments
 (0)