How to connect Glitch to mlab?

So I’ve been struggling with the FCC back end projects but been making slow progress trying to understand everything. One thing I’ve been stuck on once I tried to integrate MongoDB on Glitch. I set up an account on mlab and tried to use Glitch console commands to connect my database to my project but that’s not working for me. Is there an easy way to do it?

In your mLab db’s page, there’s a URI that you can use to connect to that db. Copy that, then paste it in the .env file (be sure to fill in a db username and password) like this:


Then you can use it in your code like this (as if it’s a regular object property):

// assuming you're using Mongoose
mongoose.connect(process.env.MONGODB_URI, {useMongoClient: true});

I know this comes late, but this worked perfectly for me. Thank you!

1 Like

Now I’m currently having trouble with the Image Search Abstraction Layer project, specifically with connecting to the Google custom search engine. Do I need to put the google API key and the custom search ID in the .env file as well? Again I am using Glitch for this project. Thanks in advance.

Yes, API keys, secrets, IDs, etc. should be set as environment variables.

1 Like

LIFESAVING!!! Thanks a mil!!

I have been trying for quite a while but I am not able to connect mLab database through mongoose. I am using the below commands:

var mongo = require(“mongoose”);
mongo.connect(process.env.MONGO_URI,{useMongoClient: true});

Please help.

make sure that un env file there is no space between MONGODB_URI=“mondb/url” and check your password for special charactere like @