Skip to content

Commit 36e54c7

Browse files
authored
Changing the .env references
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
1 parent d1bd3f5 commit 36e54c7

File tree

1 file changed

+8
-25
lines changed

1 file changed

+8
-25
lines changed

src/content/3/en/part3c.md

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -389,25 +389,6 @@ const noteSchema = new mongoose.Schema({
389389
const Note = mongoose.model('Note', noteSchema)
390390
```
391391

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:
393-
394-
```
395-
MONGODB_URI="mongodb+srv://fullstack:password@db.gwcmebp.mongodb.net/?retryWrites=true&w=majority&appName=db"
396-
```
397-
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-
411392
Let's change the handler for fetching all notes to the following form:
412393

413394
```js
@@ -510,10 +491,6 @@ This way the _Note_ variable will be assigned to the same object that the module
510491
The way that the connection is made has changed slightly:
511492

512493
```js
513-
const url = process.env.MONGODB_URI
514-
515-
console.log('connecting to', url)
516-
517494
mongoose.connect(url)
518495
.then(result => {
519496
console.log('connected to MongoDB')
@@ -523,12 +500,18 @@ mongoose.connect(url)
523500
})
524501
```
525502

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-
528503
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:
529504

530505
![node output when wrong username/password](../../images/3/45e.png)
531506

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+
const url = process.env.MONGODB_URI
511+
512+
console.log('connecting to', url)
513+
```
514+
532515
There are many ways to define the value of an environment variable. One way would be to define it when the application is started:
533516

534517
```bash

0 commit comments

Comments
 (0)