Can't confirm if mongoose is connected with Atlas or not

Tell us what’s happening:
Im trying to create a new document using mongoose from my VS code app and save it in the database but every time I check the collection on Atlas cloud I find nothing .

Your code so far

Your browser information:

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

Challenge: URL Shortener Microservice

Link to the challenge:

Have you actually created a database? Because it says 0 next to the DATABASES

Edit: just to make sure

I still don’t know the difference between DBs and clusters If they are the same I’ve already created a cluster with the name cluster0

Are you not getting an error in the console when posting? I assume you are running this locally.

Start by making sure you have the correct connection string with the right user and password and make sure you have white-listed your IP (or just all IPs i.e. 0.0.0.0/0).

You should see:

Listening on port 3000
connected

In the console. Your code should work when posting using the form. I just tested it.

But also your console.log in line 14, isn’t giving you any valuable information.

When I first looked at your screenshot, it looked like it said it was connected.

But then I checked your console.log, right now you are just printing the word “connected”

console.log("connected")); 

The log order does matter.

You have:

Listening on port 3000
Error message
connected

It should be:

Listening on port 3000
connected

Before you even try to post anything it should say “connected”.

I’m almost positive it is because you are not connecting correctly. I have seen that error message before and it was because either the connection string was wrong or the IP wasn’t whitelisted.

I don’t think its neither the IP nor the Password

The user name :

the password is : 12345

and the process.env.DB_URI is

I’ve just changed the code so I can catch the err and the word connected didn’t show up


mongoose.connect(process.env.DB_URI, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  useFindAndModify: false,
  useCreateIndex: true,
}).then(()=>{
  console.log('database connected.')
}).catch((err) => console.log(err.message))

hmmmm… I am not sure why your database isn’t connecting.

maybe someone else can spot the issue.

Not sure why you aren’t connecting either. As I said, I clone the repo and tested your code and it works for me. I also get that exact same error message if I use the wrong name or password or remove the IP whitelist.

I would suggest you log out process.env.DB_URI and make sure you are getting the value and triple-check you have the correct name/password. If that doesn’t work I would check that you are not blocking the connection locally (firewall). If you use something like TCPView you should see a node process with connections.

Log out process.env.DB_URI you mean like this

and make sure you are getting the value and triple-check you have the correct name/password

I just tried the same code in Replit and it worked so I don’t think the problem is in the password or the username

If that doesn’t work I would check that you are not blocking the connection locally (firewall)

I deactivated All the firewalls on windows security but I had the same result

If you use something like TCPView you should see a node process with connections.

I installed it and this is what it shows me

Well, I just meant logging out the variable to make sure you were getting it correctly. But hardcoding the connection string works as well. On the Database Access tab did you try clicking the edit button and checking the password?

You won’t see a connection in TCPView because you are not connected. You would see a node.exe process with some connections.

Edit: I missed this. That would confirm it to be a local issue. Maybe your router is blocking it? With the same code working on Replit I don’t really know what else can cause it, except the connection being blocked somehow.

I’m guessing that this can lead to errors:

PORT = 
DB_URI = mongodb+srv...........................

Try adding a port or removing the variable declaration.

Yes I already chucked the password and confirmed by rewriting it again , and as I said the same code worked in Replit and I saw the result on mongo db Atlas

I was thinking that as well at one point, but I tested it and it didn’t make any difference.

Yeah just tested it too…

If you have MongoDB installed locally as well try the code with a local DB. If that works it must be the connection.

If it is not the connection directly being blocked it might be the DNS being blocked. You can try using another DNS provider to test this. And as I said, if you are behind a router it can have firewall settings as well.

1 Like