MongoDB and Mongoose - Install and Set Up Mongoose

Tell us what’s happening:

I have tried the other suggested solutions and none of them solve my problem.
I’m running on github.

.ENV lines:

MONGO_URI='mongodb+srv://***:***@mongodbexercise.oht5rwr.mongodb.net/?retryWrites=true&w=majority&appName=MongodbExercise9

myAPP.js lines

const mongoose = require('mongoose')

mongoose.connect(process.env.MONGO_URI, { 
  useNewUrlParser: true, 
  useUnifiedTopology: true,
}, ()=>{
  console.log("db connection succesful")
});

my terminal reply:

gitpod /workspace/boilerplate-mongomongoose (main) $ npm run start

> fcc-mongo-mongoose-challenges@0.0.1 start
> node server.js

Your app is listening on port 3000
db connection succesful
GET
GET

Those 2 gets are the test running from what I get.
And this is the result on the tester.
[PASS] “mongoose version ^5.11.15” dependency should be in package.json
[FAILED]:“mongoose” should be connected to a database

and my mongoose shows this:

I tried other solutions, even another than added a report when connection failed but none solved my issue. Please help me see what I’m not getting.

###Your project link(s)

solution: https://3000-freecodecam-boilerplate-xl7na3aivh3.ws-us110.gitpod.io

Your browser information:

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

Challenge Information:

MongoDB and Mongoose - Install and Set Up Mongoose

I corrected the .env file to end in ’ and also tried with ". None of those worked.

right now the latest I get is this in the terminal:

(node:903) [DEP0170] DeprecationWarning: The URL mongodb://:@ac-xs6jiue-shard-00-00.oht5rwr.mongodb.net:27017,ac-xs6jiue-shard-00-01.oht5rwr.mongodb.net:27017,ac-xs6jiue-shard-00-02.oht5rwr.mongodb.net:27017/?authSource=admin&replicaSet=atlas-gz9dc3-shard-0&retryWrites=true&w=majority&appName=MongodbExercise&ssl=true is invalid. Future versions of Node.js will throw an error.
(Use node --trace-deprecation ... to show where the warning was created)
db connection succesful
GET
GET

The code you posted is passing for me.

I assume you didn’t make any other changes and left the server.js file alone.

All the test does is hit the /is-mongoose-ok endpoint found in server.js and then return the result of !!mongoose.connection.readyState


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

Thanks for your reply. From what I gathered it’s a update issue.

from what I read my mongo is an older version

but when I try to update the dependency this happens, which is a recommendation I got from you in another post.

Can you help me figure out how to update properly?

Also this happens if I try to update mongo to latest as per your suggestion here:


You should not update the mongoose dependency, as its initial version is one of the requirements (has to be ^5.11.15).

someDependency is not a package name, it is a placeholder used as an example of “some dependency” (i.e. any dependency it could possibly be).

The issue is not with mongoose or the code you posted so it must be something else. The connection log and the GET suggests that you are connected, and the test is hitting the endpoint.

Try submitting it in Incognito mode or a different browser.

I would complete it locally, but you can also try using Replit.

I tried replit and still get the Failed:“mongoose” should be connected to a database

I don’t know how to launch locally in vscode.

Post your Replit.

I don’t see your callback log for the connection. On Replit you have to use the secrets for the connection string, not an env file, and you have to submit the URL you get from clicking the “New tab” button.

Don’t update the dependencies.


The article I linked to explain how to run it locally.

  1. Install Git and Node.js

  2. Clone the repo down (or just download it).

  3. Create the env file with your connection string and add your code (remember to use dotenv).

  4. Run npm i && npm start in the terminal from the root folder.

  5. Submit http://localhost:3000

So I tried using the secrets tab and got this:

Still get the same error and now I can’t see a connection to the DB. I have Git and Node.js, I will try local install tomorrow since I was still unable to make it work.

Sorry, but I’m super ignorant and need a bit of help when you tell me to do something, please try to be as specific as you can. Thank you and sorry for bothering so much

Can I have a mongoose side error?

I finally made the local version run, all good, the report of the terminal is not returning the console.log(“db connection succesful”).

When I look at the connection charts there’s nothing there (the picture is older, but the environment looks the same:


but when I check the activity feed I see connections:

Also tried changing my mongo_uri addres:
FROM
MONGO_URI=‘mongodb+srv://***:***c@mongodbexercise.oht5rwr.mongodb.net/?retryWrites=true&w=majority&appName=MongodbExercise’

TO
MONGO_URI=‘mongodb+srv://***:***c@mongodbexercise.oht5rwr.mongodb.net/?retryWrites=true&w=majority’
which I saw some other youtube videos used as the address. This reproduced the same error message that I got in github and replit.

You can try using the catch on connect.

mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => console.log("Connected to MongoDB"))
  .catch(err => console.error(err));

You can also listen for event on the connection, including the 'error' and 'disconnected' events

mongoose docs: Error Handling


Did you setup MongoDB Atlas as described in the article? Did you update the IP allowed list to accept all IPs?

Try adding a DB name to the connection string

.env

MONGO_URI=mongodb+srv://someName:somePassword@cluster0.blabla.mongodb.net/someDBName?retryWrites=true&w=majority

2 Likes

Thanks! this finally solved my issue

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.