Can't connect to MongoDB from Glitch

can’t connect my mongodb to my freecodecamp project , need help here. https://fifth-blade.glitch.me

Hey @RobotFights, welcome to the Glitch forum!

From the error message I can see in the logs it looks like you had an authentication failure, which typically means that your username or password are incorrect. However that was from a bit ago so it’s hard to know if you’re still seeing that problem.

If you’ve resolved that and are still having trouble connecting, can you let us know what your IP address whitelist looks like for your MangoDB instance? If you’ve not opened it up to the whole internet then MongoDB may be refusing connections from your project. Since Glitch containers can theoretically be given any IP address in the Amazon AWS us-east-1 IP Address pool, and since they might change as often as twice a day, it’s very difficult to whitelist just your project’s IP.

@cori Here is my cluster whitelist IP: 0.0.0.0/0 (includes your current IP address). still having same problem.

That seems correct, so in that case I’d suggest verifying your credentials. I see the error in your logs from around an hour ago mentioning the authentication failure, and when I Remixed your project and connected it to my MongoDB instance I was able to connect just fine. As your project is currently configured the Mongo connection will only be attempted on project startup, so after I configured my Atlas cluster to accept all incoming traffic and verified the credentials in my connection string in .env, I removed and readded a comma in package.json to trigger a restart, and everything seems to connect just fine.

@cori I have done all this and still not working, as my whitelist IP accepts all .

Hey @RobotFights, can you help me understand what’s making you believe your project is not connecting?

I made a new Remix of that project, set the connection string appropriately, and restarted the project by adding/removing a character in package.js and received exactly the logging patterns that I see in your project when it restarts. When I change the password in the connection url to an invalid value I get an error (MongoNetworkError: failed to connect to server) when the project starts, so I believe that your connection string is correct and that your project is connecting just fine.

Currently your project doesn’t do anything with the MongoDB connection aside from trying to connect (and only when the project is started) - as far as I can tell you’re not doing anything with that connection yet. What are you seeing that tells you the connection isn’t successful?

@cori Am using it for my freecodecamp tutorial, and i’ve done everything accordingly but it keeps showing “not found” when i put in the live app link. and my database is well set up(mongodb).

Aha! Thanks so much! I was totally not looking at that issue!

I think what’s going on here is that your project doesn’t define an Express route for the “root” url of your project, so your project is listening, but your project doesn’t tell it what to do when it receives a request.

Adding something like

app.get('/', function( req, resp) {
  resp.send('OK!');
} );

should get it responding to incoming requests and ready to do whatever else your FCC exercise expects.

Hope this helps!

1 Like

@cori I just kept the code in myApp.js file and still not working…

I see the correct route in there, and it looks like it’s responding with “OK!” as you instructed it to. What precisely isn’t working?

@cori It doesn’t pass the freecodecamp test, and console log ‘Not found’ instead.

I see, ok. I think we’ve established that Glitch isn’t causing issues with your current work, so I think this is more along the lines of a Coding Help question at this point; I suggest creating a new topic over there to see if anyone who’s worked through the FCC exercises might have some advice to give.

1 Like

@cori Others having this issue have been resolved on the forum, and since am using the same code i don’t know why mine isn’t working. thanks for the effort, plus i taught you checked my code to see the issue.

I don’t mean to dismiss your issue, and I’m not sure where you’ve seen this FCC issue resolved in the forum - I’d love a pointer if you have one.

I did work through the issues with your code with you to resolve some common Glitch and Node problems, but I’m not familiar enough with the FCC tests to know what’s going on there, and I don’t see the console.log() message you mentioned.

Mostly I wanted to make sure that other folks who might be more familiar with FreeCodeCamp would see your question, since I think it’s pretty common for others not to respond in as much detail if Glitch support staff are involved.

@cori This is the forum link : https://www.freecodecamp.org/forum/t/mongodb-and-mongoose-install-and-set-up-mongoose/203270 and this is my live app page link: https://fifth-blade.glitch.me I hope it gets resolved soon.

@cori Been having this issue for months now, and thus makes me stagnant. Would appreciate if there’s a solution to the issue so i can move on.

@RobotFights I’d be happy to help with this! Could you create a new post under Coding Help and tag me in it? I can look into it tonight.

@RobotFights where is your database hosted, example MongoDB Atlas, or another host? That can make a difference to the connection string used.

@mishavee Mine is hosted at MongoDB Atlas . it is configured to accept all I.P’s. here is the link to my project using live app https://fifth-blade.glitch.me . Thanks

@RobotFights Atlas is pretty cool, by default it sets up a DNS which connects through to one of several servers for load balancing. That makes the connection string more complicated, but Atlas constructs it for you, see screenshots below.

Mine looks like:

mongodb+srv://MONGODB_USER:MONGODB_PASS@MONGODB_HOST/MONGODB_DB?retryWrites=true&w=majority

If the username or password has special characters that are understood by URLs, such as colon or dollar sign, you can encode them before joining them together in a string:

"mongodb+srv://" + encodeURIComponent(process.env.MONGODB_USER) + ":" + encodeURIComponent(process.env.MONGODB_PASS) + "@" + process.env.MONGODB_HOST + "/" + process.env.MONGODB_DBNAME + "?retryWrites=true&w=majority"
Screenshots

2019-08-28%2022_28_54-Clusters%20_%20Atlas_%20MongoDB%20Atlas

2019-08-28%2022_29_25-Clusters%20_%20Atlas_%20MongoDB%20Atlas

2019-08-28%2022_30_18-Clusters%20_%20Atlas_%20MongoDB%20Atlas

1 Like