MongoDB and Mongoose - Install and Set Up Mongoose

Tell us what’s happening:

I cannot get my program to connect to the database, and I’m not sure how to go about troubleshooting. The Hint provided, and online resources have been unable to help.
Here is a snapshot of my workspace: Dashboard

Your code so far

const mongoose = require(‘mongoose’);
mongoose.connect(process.env.MONGO_URI);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

Challenge Information:

MongoDB and Mongoose - Install and Set Up Mongoose

Hi @legitmanley

Did you add the .env file?

Happy coding

Just to be clear, you can’t use the sample.env file, it has to be a file named .env

Yes there is a .env file, it should be in my snapshot, but I’m not entirely sure how they work. There is another forum post on this specific step and it is in line with everything I currently have, so I’m not sure where the problem lies.

I am aware of that, there is a .env file, it should be in my snapshot, but I’m not entirely sure how they work. There is another forum post on this specific step and it is in line with everything I currently have, so I’m not sure where the problem lies.

I assume you are the one that replaced the password in the connection string? Which is the correct thing to do when sharing the workspace, I’m just checking to make sure it isn’t actually what you are using.

Anyway, if I replace your connection string with my own, your code from the workspace you shared passes the test for me.

If I’m understanding you correctly, yes I am.
If your connection string works, does that mean I have set the database up incorrectly?
And if so, how would I fix it?

Did you whitelist all IPs?

Have you followed the tutorial?

Try this connection string, update the name and password.

mongodb+srv://yourName:yourPassword@jmanley-cluister1.wm82e.mongodb.net/fccChallenges?retryWrites=true&w=majority

As said, all I have to do is update the .env file in your code with my own connection string, and it passes the tests.

I did follow that tutorial, and I believe I have whitelisted all IP’s. 0.0.0.0/0 is in the access list.
I tried using the connection string you gave me, updated with my name and password, but the second test is still failing.

Post the URL you are submitting. Have the Gitpod code open and running.

You could try adding some logging to it.

mongoose.connect(process.env.MONGO_URI).then(() => {
  console.log("Connected to DB: ", mongoose.connection.readyState); // should say, Connected to DB: 1
}).catch(err => console.error(err));

You might also try running it locally instead. Just to test it.

I am running it locally, I’m submitting localhost:3000 which worked for previous sections.
I am using VSCode desktop if that is of any relevance.

Did you just now switch to running locally?

Did you try using Gitpod at all? The only code I have tested is your Gitpod code and as said it works for me.

Did you log the connection?

Yes I’m using Gitpod, just connecting it to my desktop VSCode instead of using the browser version and the gitpod workspace is running.
I did add the logging message but nothing is appearing in the console.

If nothing is logged, it isn’t connecting.

Don’t use Gitpod locally. Use it on their site.

If you want to do it locally, clone down the repo and do it that way.

if your server is on gitpod I think you need to use the gitpod port url

I’ve tried it using their site, submitting this URL:
https://3000-freecodecam-boilerplate-235ospet4ne.ws-us117.gitpod.io

However, it still fails the second check.