Skip to content

Commit e1347a3

Browse files
committed
Update outdated code sample
1 parent de59590 commit e1347a3

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/content/4/en/part4c.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,8 @@ The code for creating a new note has to be updated so that the note is assigned
431431
Let's expand our current implementation in <i>controllers/notes.js</i> so that the information about the user who created a note is sent in the <i>userId</i> field of the request body:
432432

433433
```js
434+
const notesRouter = require('express').Router()
435+
const Note = require('../models/note')
434436
const User = require('../models/user') //highlight-line
435437

436438
//...
@@ -442,16 +444,18 @@ notesRouter.post('/', async (request, response) => {
442444

443445
const note = new Note({
444446
content: body.content,
445-
important: body.important === undefined ? false : body.important,
447+
important: body.important || false,
446448
user: user._id //highlight-line
447449
})
448450

449451
const savedNote = await note.save()
450452
user.notes = user.notes.concat(savedNote._id) //highlight-line
451453
await user.save() //highlight-line
452-
454+
453455
response.status(201).json(savedNote)
454456
})
457+
458+
// ...
455459
```
456460

457461
It's worth noting that the <i>user</i> object also changes. The <i>id</i> of the note is stored in the <i>notes</i> field of the <i>user</i> object:

src/content/4/fi/osa4c.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,9 @@ Muistiinpanot luovaa koodia on nyt mukautettava siten, että uusi muistiinpano t
422422
Laajennetaan ensin olemassaolevaa toteutusta siten, että tieto muistiinpanon luovan käyttäjän id:stä lähetetään pyynnön rungossa kentän <i>userId</i> arvona:
423423

424424
```js
425-
const User = require('../models/user')
425+
const notesRouter = require('express').Router()
426+
const Note = require('../models/note')
427+
const User = require('../models/user') //highlight-line
426428

427429
//...
428430

@@ -433,16 +435,18 @@ notesRouter.post('/', async (request, response) => {
433435

434436
const note = new Note({
435437
content: body.content,
436-
important: body.important === undefined ? false : body.important,
438+
important: body.important || false,
437439
user: user._id //highlight-line
438440
})
439441

440442
const savedNote = await note.save()
441443
user.notes = user.notes.concat(savedNote._id) //highlight-line
442444
await user.save() //highlight-line
443445

444-
response.json(savedNote)
446+
response.status(201).json(savedNote)
445447
})
448+
449+
// ...
446450
```
447451

448452
Huomionarvoista on nyt se, että myös <i>user</i>-olio muuttuu. Sen kenttään <i>notes</i> talletetaan luodun muistiinpanon <i>id</i>:

0 commit comments

Comments
 (0)