Skip to content

Commit 82c3ee8

Browse files
committed
Clarify part 3c
1 parent 01519bf commit 82c3ee8

File tree

2 files changed

+14
-42
lines changed

2 files changed

+14
-42
lines changed

src/content/3/en/part3c.md

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,9 @@ Let's get a quick start by copy-pasting the Mongoose definitions to the <i>index
360360
```js
361361
const mongoose = require('mongoose')
362362

363-
const password = process.argv[2]
364-
365363
// DO NOT SAVE YOUR PASSWORD TO GITHUB!!
366-
const url =
367-
`mongodb+srv://fullstack:${password}@cluster0.a5qfl.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0`
364+
const password = process.argv[2]
365+
const url = `mongodb+srv://fullstack:${password}@cluster0.a5qfl.mongodb.net/noteApp?retryWrites=true&w=majority&appName=Cluster0`
368366

369367
mongoose.set('strictQuery',false)
370368
mongoose.connect(url)
@@ -377,25 +375,6 @@ const noteSchema = new mongoose.Schema({
377375
const Note = mongoose.model('Note', noteSchema)
378376
```
379377

380-
To avoid authentication issues with the password variable in index.js, we need to create a .env file by running npm install dotenv in the command line. Then, let's create the .env file in the root of your directory. In that file, you should place your URI:
381-
382-
```
383-
MONGODB_URI="mongodb+srv://fullstack:[email protected]/?retryWrites=true&w=majority&appName=Cluster0"
384-
```
385-
Don't forget to replace the string with your details.
386-
Once the .env file is ready, remember to add it to your .gitignore file to prevent pushing the password to Git:
387-
388-
```
389-
/node_modules
390-
.env
391-
```
392-
Then, in your index.js file, make the necessary changes with the following line so that your code can access the URL in your .env file:
393-
394-
```
395-
const url = process.env.MONGODB_URI;
396-
397-
```
398-
399378
Let's change the handler for fetching all notes to the following form:
400379

401380
```js
@@ -406,17 +385,13 @@ app.get('/api/notes', (request, response) => {
406385
})
407386
```
408387

409-
We can verify in the browser that the backend works for displaying all of the documents:
388+
Let's start the backend with the command <code>node --watch index.js yourpassword</code> so we can verify in the browser that the backend correctly displays all notes saved to the database:
410389

411390
![api/notes in browser shows notes in JSON](../../images/3/44ea.png)
412391

413392
The application works almost perfectly. The frontend assumes that every object has a unique id in the <i>id</i> field. We also don't want to return the mongo versioning field <i>\_\_v</i> to the frontend.
414393

415-
One way to format the objects returned by Mongoose is to [modify](https://stackoverflow.com/questions/7034848/mongodb-output-id-instead-of-id) the _toJSON_ method of the schema, which is used on all instances of the models produced with that schema.
416-
417-
To modify the method we need to change the configurable options of the schema, options can be changed using the set method of the schema, see here for more info on this method: https://mongoosejs.com/docs/guide.html#options. See <https://mongoosejs.com/docs/guide.html#toJSON> and <https://mongoosejs.com/docs/api.html#document_Document-toObject> for more info on the _toJSON_ option.
418-
419-
see <https://mongoosejs.com/docs/api/document.html#transform> for more info on the _transform_ function.
394+
One way to format the objects returned by Mongoose is to [modify](https://stackoverflow.com/questions/7034848/mongodb-output-id-instead-of-id) the _toJSON_ method of the schema, which is used on all instances of the models produced with that schema. Modification can be done as follows:
420395

421396
```js
422397
noteSchema.set('toJSON', {
@@ -549,9 +524,9 @@ Let's change the <i>index.js</i> file in the following way:
549524
```js
550525
require('dotenv').config() // highlight-line
551526
const express = require('express')
552-
const app = express()
553-
const Note = require('./models/note') // highlight-line
527+
const Note = require('./models/note')
554528

529+
const app = express()
555530
// ..
556531

557532
const PORT = process.env.PORT // highlight-line
@@ -562,7 +537,7 @@ app.listen(PORT, () => {
562537

563538
It's important that <i>dotenv</i> gets imported before the <i>note</i> model is imported. This ensures that the environment variables from the <i>.env</i> file are available globally before the code from the other modules is imported.
564539

565-
### Important note to Fly.io users
540+
### Important note about environment variables
566541

567542
Because GitHub is not used with Fly.io, the file .env also gets to the Fly.io servers when the app is deployed. Because of this, the env variables defined in the file will be available there.
568543

@@ -578,8 +553,6 @@ and set the env value from the command line with the command:
578553
fly secrets set MONGODB_URI="mongodb+srv://fullstack:[email protected]/noteApp?retryWrites=true&w=majority&appName=Cluster0"
579554
```
580555

581-
Since the PORT also is defined in our .env it is actually essential to ignore the file in Fly.io since otherwise the app starts in the wrong port.
582-
583556
When using Render, the database url is given by defining the proper env in the dashboard:
584557

585558
![browser showing render environment variables](../../images/3/render-env.png)

src/content/3/fi/osa3c.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,10 @@ Aloitetaan nopean kaavan mukaan, copy-pastetaan tiedostoon <i>index.js</i> Mongo
353353
```js
354354
const mongoose = require('mongoose')
355355

356+
356357
// ÄLÄ KOSKAAN TALLETA SALASANOJA GitHubiin!
357-
const url =
358-
`mongodb+srv://fullstack:${password}@cluster0.a5qfl.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0`
358+
const password = process.argv[2]
359+
const url = `mongodb+srv://fullstack:${password}@cluster0.a5qfl.mongodb.net/noteApp?retryWrites=true&w=majority&appName=Cluster0`
359360

360361
mongoose.set('strictQuery',false)
361362
mongoose.connect(url)
@@ -378,7 +379,7 @@ app.get('/api/notes', (request, response) => {
378379
})
379380
```
380381

381-
Voimme todeta selaimella, että backend toimii kaikkien dokumenttien näyttämisen osalta:
382+
Käynnistetään nyt backend komennolla <code>node --watch index.js yourpassword</code>, jotta voimme varmistua koodin toimivuudesta. Voimme todeta selaimella, että backend toimii kaikkien dokumenttien näyttämisen osalta:
382383

383384
![Mongoon tallennetut muistiinpanot renderöityvät selaimeen JSON-muodossa](../../images/3/44ea.png)
384385

@@ -518,9 +519,9 @@ Muutetaan nyt tiedostoa <i>index.js</i> seuraavasti:
518519
```js
519520
require('dotenv').config() // highlight-line
520521
const express = require('express')
521-
const app = express()
522-
const Note = require('./models/note') // highlight-line
522+
const Note = require('./models/note')
523523

524+
const app = express()
524525
// ..
525526

526527
const PORT = process.env.PORT // highlight-line
@@ -531,7 +532,7 @@ app.listen(PORT, () => {
531532

532533
On tärkeää, että <i>dotenv</i> otetaan käyttöön ennen modelin <i>note</i> importtaamista. Tällöin varmistutaan siitä, että tiedostossa <i>.env</i> olevat ympäristömuuttujat ovat alustettuja kun moduulin koodia importoidaan.
533534

534-
### Tärkeä huomio Fly.io:n käyttäjille
535+
### Tärkeä huomio ympäristömuuttujista
535536

536537
Koska Fly.io ei hyödynnä gitiä, menee myös .env-tiedosto Fly.io:n palvelimelle, ja ympäristömuuttujien arvo välittyy myös sinne.
537538

@@ -547,8 +548,6 @@ ja asettaa ympäristömuuttujan arvo komennolla:
547548
fly secrets set MONGODB_URI='mongodb+srv://fullstack:[email protected]/noteApp?retryWrites=true&w=majority&appName=Cluster0'
548549
```
549550

550-
Koska .env-tiedosto määrittelee myös ympäristömuuttujan PORT arvon, on .env:in ignorointi oikeastaan välttämätöntä jotta sovellus ei yritä käynnistää itseään väärään portiin.
551-
552551
Renderiä käytettäessä tietokannan osoitteen kertova ympäristömuuttuja määritellään dashboardista käsin:
553552

554553
![](../../images/3/render-env.png)

0 commit comments

Comments
 (0)