Skip to content

Commit 506bcdd

Browse files
committed
Add error handling if user doesn't exist
1 parent a0d2342 commit 506bcdd

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/content/4/en/part4c.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,13 @@ const User = require('../models/user') //highlight-line
440440
notesRouter.post('/', async (request, response) => {
441441
const body = request.body
442442

443-
const user = await User.findById(body.userId) //highlight-line
443+
const user = await User.findById(body.userId)// highlight-line
444+
445+
// highlight-start
446+
if (!user) {
447+
return response.status(400).json({ error: 'userId missing or not valid' })
448+
}
449+
// highlight-end
444450

445451
const note = new Note({
446452
content: body.content,
@@ -458,6 +464,8 @@ notesRouter.post('/', async (request, response) => {
458464
// ...
459465
```
460466

467+
The database is first queried for a user using the <i>userId</i> provided in the request. If the user is not found, the response is sent with a status code of 400 (<i>Bad Request</i>) and an error message: <i>"userId missing or not valid"</i>.
468+
461469
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:
462470

463471
```js
@@ -483,6 +491,8 @@ Likewise, the ids of the users who created the notes can be seen when we visit t
483491

484492
![api/notes shows ids of users in JSON](../../images/4/12e.png)
485493

494+
Due to the changes we made, the tests no longer pass, but we leave fixing the tests as an optional exercise. The changes we made have also not been accounted for in the frontend, so the note creation functionality no longer works. We will fix the frontend in part 5 of the course.
495+
486496
### Populate
487497

488498
We would like our API to work in such a way, that when an HTTP GET request is made to the <i>/api/users</i> route, the user objects would also contain the contents of the user's notes and not just their id. In a relational database, this functionality would be implemented with a <i>join query</i>.
@@ -559,6 +569,4 @@ const noteSchema = new mongoose.Schema({
559569

560570
You can find the code for our current application in its entirety in the <i>part4-8</i> branch of [this GitHub repository](https://github.com/fullstack-hy2020/part3-notes-backend/tree/part4-8).
561571

562-
NOTE: At this stage, firstly, some tests will fail. We will leave fixing the tests to a non-compulsory exercise. Secondly, in the deployed notes app, the creating a note feature will stop working as user is not yet linked to the frontend.
563-
564572
</div>

src/content/4/fi/osa4c.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,13 @@ const User = require('../models/user') //highlight-line
431431
notesRouter.post('/', async (request, response) => {
432432
const body = request.body
433433

434-
const user = await User.findById(body.userId) //highlight-line
434+
const user = await User.findById(body.userId)// highlight-line
435+
436+
// highlight-start
437+
if (!user) {
438+
return response.status(400).json({ error: 'userId missing or not valid' })
439+
}
440+
// highlight-end
435441

436442
const note = new Note({
437443
content: body.content,
@@ -449,6 +455,8 @@ notesRouter.post('/', async (request, response) => {
449455
// ...
450456
```
451457

458+
Tietokannasta etsitään ensin käyttäjä pyynnön mukana lähetetyn <i>userId</i>:n avulla. Jos käyttäjää ei löydy, vastataan statuskoodilla 400 (<i>Bad Request</i>) ja virheviestillä <i>"userId missing or not valid"</i>.
459+
452460
Huomionarvoista on nyt se, että myös <i>user</i>-olio muuttuu. Sen kenttään <i>notes</i> talletetaan luodun muistiinpanon <i>id</i>:
453461

454462
```js
@@ -474,6 +482,8 @@ Muistiinpanon luoneen käyttäjän id tulee näkyviin muistiinpanon yhteyteen:
474482

475483
![Selain renderöi osoitteessa localhost:3001/api/notes taulukollisen JSON:eja joilla kentät content, important, id ja user, jonka arvo käyttäjäid](../../images/4/12e.png)
476484

485+
Tekemiemme muutosten myötä testit eivät mene enää läpi, mutta jätämme testien korjaamisen nyt vapaaehtoiseksi harjoitustehtäväksi. Tekemiämme muutoksia ei ole myöskään huomioitu frontendissä, joten muistiinpanojen luomistoiminto ei enää toimi. Korjaamme frontendin kurssin osassa 5.
486+
477487
### populate
478488

479489
Haluaisimme API:n toimivan siten, että haettaessa esim. käyttäjien tiedot polulle <i>/api/users</i> tehtävällä HTTP GET ‑pyynnöllä tulisi käyttäjien tekemien muistiinpanojen id:iden lisäksi näyttää niiden sisältö. Relaatiotietokannoilla toiminnallisuus toteutettaisiin <i>liitoskyselyn</i> avulla.

0 commit comments

Comments
 (0)