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
@@ -377,25 +375,6 @@ const noteSchema = new mongoose.Schema({
377
375
constNote=mongoose.model('Note', noteSchema)
378
376
```
379
377
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:
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:
410
389
411
390

412
391
413
392
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.
414
393
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:
420
395
421
396
```js
422
397
noteSchema.set('toJSON', {
@@ -549,9 +524,9 @@ Let's change the <i>index.js</i> file in the following way:
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.
564
539
565
-
### Important note to Fly.io users
540
+
### Important note about environment variables
566
541
567
542
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.
568
543
@@ -578,8 +553,6 @@ and set the env value from the command line with the command:
578
553
fly secrets set MONGODB_URI="mongodb+srv://fullstack:[email protected]/noteApp?retryWrites=true&w=majority&appName=Cluster0"
579
554
```
580
555
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
-
583
556
When using Render, the database url is given by defining the proper env in the dashboard:
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:
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.
533
534
534
-
### Tärkeä huomio Fly.io:n käyttäjille
535
+
### Tärkeä huomio ympäristömuuttujista
535
536
536
537
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.
537
538
@@ -547,8 +548,6 @@ ja asettaa ympäristömuuttujan arvo komennolla:
547
548
fly secrets set MONGODB_URI='mongodb+srv://fullstack:[email protected]/noteApp?retryWrites=true&w=majority&appName=Cluster0'
548
549
```
549
550
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
-
552
551
Renderiä käytettäessä tietokannan osoitteen kertova ympäristömuuttuja määritellään dashboardista käsin:
0 commit comments