Exercise Tracker Project start-up error

When I start the project using Glitch or clone the github repo I get an error straight away in the console log which prevents the index.html to load. I didn’t touch any code even. Tried starting the project multiple times with the link but still get the same error.

The error from the console log:

(node:3432) DeprecationWarning: open() is deprecated in mongoose >= 4.11.0, use openUri() instead, or set the useMongoClient option if using connect() or createConnection(). See http://mongoosejs.com/docs/4.x/docs/connections.html#use-mongo-client

4:44 PM

:ocean::ribbon: Your app is listening on port 3000

4:44 PM

4:44 PM

events.js:160

4:44 PM

throw er; // Unhandled ‘error’ event

4:44 PM

^

4:44 PM

MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]

4:44 PM

at Pool. (/rbd/pnpm-volume/69317b90-ccc6-4359-b8b2-ada3af033012/node_modules/.registry.npmjs.org/mongodb-core/2.1.18/node_modules/mongodb-core/lib/topologies/server.js:336:35)

4:44 PM

at emitOne (events.js:96:13)

4:44 PM

at Pool.emit (events.js:188:7)

4:44 PM

at Connection. (/rbd/pnpm-volume/69317b90-ccc6-4359-b8b2-ada3af033012/node_modules/.registry.npmjs.org/mongodb-core/2.1.18/node_modules/mongodb-core/lib/connection/pool.js:280:12)

4:44 PM

at Connection.g (events.js:292:16)

4:44 PM

at emitTwo (events.js:106:13)

4:44 PM

at Connection.emit (events.js:191:7)

4:44 PM

at Socket. (/rbd/pnpm-volume/69317b90-ccc6-4359-b8b2-ada3af033012/node_modules/.registry.npmjs.org/mongodb-core/2.1.18/node_modules/mongodb-core/lib/connection/connection.js:189:49)

4:44 PM

at Socket.g (events.js:292:16)

4:44 PM

at emitOne (events.js:96:13)

4:44 PM

at Socket.emit (events.js:188:7)

4:44 PM

at emitErrorNT (net.js:1290:8)

4:44 PM

at _combinedTickCallback (internal/process/next_tick.js:80:11)

4:44 PM

at process._tickCallback (internal/process/next_tick.js:104:9)

Your code so far

Your browser information:

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

Challenge: undefined

Link to the challenge:
https://www.freecodecamp.org/learn/apis-and-microservices/apis-and-microservices-projects/exercise-tracker![dfg|690x172]

I have the same exact issue. I literally just opened the project in Glitch and already have an error with connecting to the mongo database

Hi all,

I’m not sure if it’s the same issue but I had trouble when I opened this starter project too.

I altered some of the database connection code and it works fine now. This is the code I’m using:

const mongoose = require("mongoose");
mongoose.connect(process.env.MLAB_URI || "mongodb://localhost/exercise-track", {
  useNewUrlParser: true,
  useUnifiedTopology: true
});

And obviously it is taking the MLAB_URI from the .env file…

Hope it helps?

1 Like

Thanks for your help! That fixed it for me. I guess we just had to pass in those two parameters.

For anyone else reading this, just make sure you have your database connection string in the .env file and you should be good. I stored my connection string as the variable MONGO_URI because that’s what we did in the tutorials. Just make sure your variable name when you’re connecting matches the one in the .env file.

Quick theoretical question. Do you know why we need the “mongodb://localhost/exercise-track” part? Why can’t we just use our env variable and not have the OR?

If you don’t know then it’s cool. I’m just curious.

Note: @Checkist Even after I did what @zgparsons said, I was able to connect to the database but got an error saying : "Error: Invalid mongodb uri “mongodb+srv://db_user:db_user@freecodecamp-r6wuh.mongodb.net/test?retryWrites=true&w=majority”. Must begin with “mongodb://”

To fix this, I went to my package.json file and changed my mongodb dependency to “^3.0.0” and mongoose to “^5.6.5”.

Now I get no errors

Oh yep, I also forgot that I updated quite a few of the dependencies in the package.json too so that probably helps with the error messages…

The question about the env variable is one I’m not sure about, but I think that in production some databases will actually be saved onto the same server (hence localhost, rather than an env variable to a remote URI), so an or statement is used so that the code works in both pre and post deployment, but I think in this context we could probably get rid of that ‘or’ statement and it would still work fine because it’s using the env varaiable for definite each time.

Good luck with this project btw - I think I’ve just about completed it but I cannot get one of the tests to pass (even though it seems to be fulfilling the user story correctly!) I’ll have to take a break and have another look later!

Thanks - Z

Thanks, man! Yeah I finished the project, and everything seems to be working as expected, but it still isn’t passing the 3rd and 4th tests. I’ve like triple-checked it and used console.log() a bunch to try to figure out what was wrong but to no avail. However, I don’t care enough to keep scratching my head over it cause it does work. If they could show us their testing code that would be super helpful. I don’t know what they’re doing to test it, but every time I test it it works- even if it’s with their post requests. So idk but whatever. I’m not gonna waste a bunch of time trying to figure why it won’t pass their tests when it works.