MongoDB and Mongoose - Install and Set Up Mongoose

Tell us what’s happening:

I am trying to connect mongoose to the database, but it isn’t working.

I’ve got everything installed correctly, here is my code:



const mongoose = require(‘mongoose’);
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log(‘Database connected successfully’))
.catch(err => console.error('Database connection error: ', err));

MONGO_URI=‘uri is here :)’

###Your project link(s)


Your browser information:

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

Challenge Information:

MongoDB and Mongoose - Install and Set Up Mongoose

Is your MONGO_URI in the .env file? Also, I don’t think mongoose .connect() is async so .then() and .catch() might not work with it, or at least, I did not use any async with it when I did this

Yes, my MONGO_URI is in the .env file, and it said to use .connect() in the lesson.

Yeah .connect() is correct, I mean that I don’t think .then() and .catch() will work with it because those are for dealing with promises and I don’t think that .connect() returns a promise

Even if I get rid of it, it still doesn’t work.

connect returns a promise.

What doesn’t work, the connection or the tests?

What does your code log out?

We can’t see your code. If you need more help, post a GitHub repo or share a snapshot of your Gitpod workspace.

As an aside, some of Mongoose API methods return thenables as well, even when not returning a real promise.

1 Like

It works now, I’m not exactly sure what was wrong. It works when I open the gitpod workspace but breaks when I try running it.