How do I set up Mongoose (MongoDB) files by connecting to a server?

I’m trying to use Mongoose, the npm MongoDB api. However, I’m not sure how to connect to database files. The api requires you to connect to a MongoDB link. How would I connect it to my Glitch project?

EDIT: Sorry, I couldn’t give much more info since I was on mobile. This is the line of code I’m talking about:

mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true});


localhost means the database is being run on the same host as the program accessing it. Its unlikely you’ve installed a mongo database on Glitch, so you’ll need to find the connection string from where you have it hosted.

The following is how to work out the url from MongoDB Atlas, if by chance you have it hosted there.


Alright, I set it up. But now I’m getting an error.
mongoose.connect('mongodb+srv://', { useNewUrlParser: true });

This gives the error:

(node:3280) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [] on first connect [MongoNetworkError: connection 5 to closed

What do I do?

This is the error that’s returned when the database cluster (MongoDB Atlas) rejects the connection request.

In Network Access, add to the IP Whitelist, “allow access from anywhere”

(good for initial testing), or the IP address of the server.

To get the IP address, this is a quick way to do it:

Open the console, and type …

$ curl

This doesn’t need to run all the time, the IP shouldn’t change.

IP can change, see later post by Cori


Alright! Thanks! But now, I have a different error.

(node:6341) UnhandledPromiseRejectionWarning: MongoParseError: Invalid connection string

I’m pretty sure the connection string is correct, though…


(My actual username and password are put in the string.)

Looks promising!

The invalid connection string could be either of

Special characters in the username or password, these would need to be urlencoded. For one-offs, you can use . If the password might have special characters any time, you can encode them in your glitch server.js, but you’d need to separate the parts of the url so as not to encode everything.

If you are storing the entire url in .env, and it contains the & ampersand character, the string needs to be quoted, for example


My connection string contains a username and password with only letters and numbers, but it did have an &. I put the ‘s around it in the .env file. But now, I’m getting the same error I got in my second message in this thread.
I whitelisted every IP… why isn’t it authenticating?

Did you try to whitelist the IP address of your app?
Have you created the database user and given it access?

Maybe there’s something missing in the MongoDB setup. I’ll take a look if you send me the details in a private message here.

Haha, the IP address of one of my glitch apps just changed.

@cori is this expected behaviour?

@mishavee yes, Glitch project IP addresses will change with some regularity as we recycle project hosts. Your IP could change as frequently as every 12 hours when your project is stopped on our regular schedule and is pulled from all of the AWS us-east IP addresses (What IPs must I whitelist so my Glitch project can access my MongoDB database? shows a way to find that list if you’re interested - it amounts to thousands of IPs).

Given that @EBB has whitelisted all IPs I don’t think this is the issue.

1 Like

@cori is right. I did whitelist all IPs. However, do I have to create a new database user specifically for my Glitch project or can I connect using my personal username and password? I’m currently trying to connect through my own db user. If I can, I don’t get the problem… I have quadruple checked the username/password and IP whitelist.

Turns out, I solved it! I don’t know what was happening… but I created a new DB user and tried logging in with that and it worked! Woohoo! Thanks for all the help, guys.