I used replit
Sample.env
SECRET=
MADE_WITH=
MONGO_URI='mongodb+srv://myusername:<mypasswordhere>@cluster0.adzbz.mongodb.net/local_library?retryWrites=true&w=majority'
Link to .env
myApp.js
var mongodb = require('mongodb');
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI,{useNewUrlParser: true, useUnifiedTopology: true });
Link to myApp.js
Package.json
{
"name": "fcc-mongo-mongoose-challenges",
"version": "0.0.1",
"description": "A boilerplate project",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"body-parser": "^1.15.2",
"dotenv": "^8.2.0",
"express": "^4.12.4",
"mongodb": "^4.1.1",
"mongoose": "^6.0.2"
},
"repository": {
"type": "git",
"url": "https://github.com/freeCodeCamp/boilerplate-mongomongoose.git"
},
"keywords": [
"node",
"mongoose",
"express"
],
"license": "MIT"
}
Link to package.json
@Shigosag . The connection string is a secret and therefore is not suppose to be visible as it is on myApp.js.
Delete the var mongoDB from myApp.js.
Select the secrets
3.Add the key and value
Choose where you need to place the code on myApp.js
Click the insert button to add the variable.
Then const mySecret = process.env[‘MONGO_URI’] will be placed on the line that you selected. From there then you can be able to referenced env from mySecret variable.
1 Like
Okay. I just delete var mongoDB
And thank you
But, must I surround the URI with single or double quotes in secret .env value?
The URI must not be surrounded by any quotes in secret .env value.
Okay.
Am I supposed to paste
const mySecret = process.env['MONGO_URI']
In myApp.js or package.jon or am gonna leave it after I saved secret .env
Is my mongodb and mongoose version in dependencies correct?
"mongodb": "^4.1.1",
"mongoose": "^6.0.2"
And, is myApp.js correct?
var mongodb = require('mongodb');
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI,{useNewUrlParser: true});
A few things.
No.1:
Delete everything in your sampl.env file.
You still have your secret mongo uri key in there.
It is important you do not share that information publicly.
No.2:
You are not following instructions here
You are not supposed to write any code below the exports.
Move all of this
to the very top of your file.
Not the bottom.
No.3:
You can use the mySecret variable in your mongoose.connect
mongoose.connect(mySecret, { useNewUrlParser: true, useUnifiedTopology: true });
No.4:
In your mongo uri, you don’t need to wrap your password in <>
You can just write this
mongodb+srv://myusername:mypasswordhere
mongoose has not connected to a database yet.
Do you mean I should replace
With
In myApp.js
Or it should be
mongoose.connect(<my URI>, { useNewUrlParser: true, useUnifiedTopology: true });
Secret .env value look like this after I removed wrap <>
mongodb+srv://myusername:mypasswordhere@cluster0.adzbz.mongodb.net/local_library?retryWrites=true&w=majority
And the secret .env console shows “your app is listening on port 3000”
Then, I’m not sure if I should replace
With
"mongodb": "^3.0.0"
In myApp.js
This line of code
require('dotenv').config();
loads your environment variables into a process.env
When you added this line
const mySecret = process.env['MONGO_URI']
now we have access your MONGO_URI value without exposing it to the public.
When you connect the database, you can just reference the variable.
mongoose.connect(mySecret, { useNewUrlParser: true, useUnifiedTopology: true });
As for your package.json file, you can use these versions.
"mongodb": "^4.1.1",
"mongoose": "^6.0.2"
The test will pass with those versions.
That looks right.
You should have copied the mongo uri from your atlas account and then just added your password.
Everything else is the same.
2 Likes
Everything has been solved.
Thank you so much
I was just giving myself problem before by using different tab on chrome to run .env and myApp.js , and It later worked with the solution you provided when i setup new .env and myApp.js on single tab.
1 Like
system
Closed
February 27, 2022, 12:28am
14
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.