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
The way it was done originally was kind of confusing, because .env is mentioned at the beggining, without explaining that it must be imported (required). Then, the rest of the explanation was given below. This way, .env is mentioned when the whole explanation is given
Copy file name to clipboardExpand all lines: src/content/3/en/part3c.md
+8-25Lines changed: 8 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -389,25 +389,6 @@ const noteSchema = new mongoose.Schema({
389
389
constNote=mongoose.model('Note', noteSchema)
390
390
```
391
391
392
-
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:
Don't forget to replace the string with your details.
398
-
Once the .env file is ready, remember to add it to your .gitignore file to prevent pushing the password to Git:
399
-
400
-
```
401
-
/node_modules
402
-
.env
403
-
```
404
-
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:
405
-
406
-
```
407
-
const url = process.env.MONGODB_URI;
408
-
409
-
```
410
-
411
392
Let's change the handler for fetching all notes to the following form:
412
393
413
394
```js
@@ -510,10 +491,6 @@ This way the _Note_ variable will be assigned to the same object that the module
510
491
The way that the connection is made has changed slightly:
511
492
512
493
```js
513
-
consturl=process.env.MONGODB_URI
514
-
515
-
console.log('connecting to', url)
516
-
517
494
mongoose.connect(url)
518
495
.then(result=> {
519
496
console.log('connected to MongoDB')
@@ -523,12 +500,18 @@ mongoose.connect(url)
523
500
})
524
501
```
525
502
526
-
It's not a good idea to hardcode the address of the database into the code, so instead the address of the database is passed to the application via the <em>MONGODB_URI</em> environment variable.
527
-
528
503
The method for establishing the connection is now given functions for dealing with a successful and unsuccessful connection attempt. Both functions just log a message to the console about the success status:
529
504
530
505

531
506
507
+
It's also not a good idea to hardcode the address of the database into the code, so the url is obtained differently: the address of the database is passed to the application via the <em>MONGODB_URI</em> environment variable:
508
+
509
+
```js
510
+
consturl=process.env.MONGODB_URI
511
+
512
+
console.log('connecting to', url)
513
+
```
514
+
532
515
There are many ways to define the value of an environment variable. One way would be to define it when the application is started:
0 commit comments