FCC - Exercise tracker - trouble with packages, POST, etc

Hi all,

I’m working on the backend/API course, which will be my third FCC course that I’ve taken. I’m having a few issues, related to the exercise tracker, that I hvaen’t seen yet. I know that one challenge in microservices is keeping up with the always changing landscape of packages, hoping the issues are related. Short recap of what is going on:

I was almost done with solving the first criterion, “You can POST to /api/users with form data username to create a new user.” Somewhere DLing packages, etc, I started to get lots of error that I was assuming were an issue with my Database, or package versions, or something else under the hood:

    throw err;
Error: Cannot find module 'express'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/app/server.js:1:17)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)

2nd Attempt, copy a new Glitch project, import from the Github starting project. The second I added const mongoose = require('mongoose') after DLing the mongodb and mongoose packages, I got similar errors. I made sure to require the mongoose and mongo db versions that were in the previous exercise (url shortener), not knowing how much these courses keep up with node and mongo updates, to no avail.

3rd attempt, today, from scratch. No errors, everything on the debug window looks right, however my mongoose.connection.readyState keeps giving me a ‘2’, which means ‘connecting’, haven’t seen a 1 yet, and every time I try to put data in api/users, I just get ‘’'cannot POST /api/users```.

So, I’m getting a little frustrated, not sure what I’m doing wrong. Here are the relevant code snippets:

const app = express()
const cors = require('cors')
const shortId = require('shortid')

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/views/index.html')

//DB connection
const mongoose = require('mongoose');
const myUriCode = 'mongodb+srv://<username>:<pass>@free-code-camp-3.vhtmx.mongodb.net/myFirstDatabase?retryWrites=true&w=majority';
const myUri = process.env.MONGO_URI;
mongoose.connect(myUriCode, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true });

const listener = app.listen(process.env.PORT || 3000, () => {
  console.log('Your app is listening on port ' + listener.address().port)

const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: false}));

//Schema, Model
const Schema = new mongoose.Schema({
  shortId: {type: String, unique: true, default: shortId.generate},
  username: String,
  exercise: [{
    desc: String,
    duration: Number,
    date: {}
const exerDb = mongoose.model('exerDb', Schema);
//Check Mongoose connection
console.log(mongoose.connection.readyState, (err, data) => {
  if(err) {
    console.log("the error is " + err)
  } else {
    console.log("the data is " + data)

//trying separate function to create person
const createAPerson = (name, done) => {
  exerDb.findOne({username: name}, (err,findData) => {
    if (findData == null) {
      const person = new exerDb({username: name, exercise: []});
      exerDb.save((err, data)=> {
        if(err) {
        done(null, data);
    } else if (err) {
    } else {
      done(null, "taken")

//"You can POST to /api/users with form data username to create a new user."
app.post('api/users/:username', (req,res) => {


  "name": "fcc-exercise-tracker",
  "version": "0.1.0",
  "description": "A REST API project, part of Free Code Camp's curriculum",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  "dependencies": {
    "dotenv": "^8.2.0",
    "express": "^4.16.4",
    "cors": "^2.8.5",
    "mongodb": "^3.6.0",
    "mongoose": "^5.4.0",
    "shortid": "^2.2.16"
  "repository": {
    "url": "https://github.com/freeCodeCamp/boilerplate-project-exercisetracker"
  "license": "MIT",
  "keywords": [


Please include a single post with a specific question, as it is now, it quite broad and not very clear what the main problem is.

Hello there,

A link to your Glitch/Replit/GitHub would probably be better than some of the code.

This error usually means you need to run npm i within the terminal.

This is because you have declared the post route after the app.listen. The route might as well not exist, in this instance.

Finally, I have edited your post to remove your URI password. This should be kept as safe as any other services password, unless you want others to be able to abuse your database account.

Hope this helps

1 Like

Thank you for your detailed response! Here is the glitch project, this time with PW taken out of server.js - Glitch :・゚✧

Here’s a silly question, is this the terminal on the Glitch project, or my computer? The Glitch projects don’t interact with installed packages on my computer, do they?

I moved the app.listen down to the bottom, and still getting the same error.

Doh! Of course, thank you so much!

If you are working within Glitch to host your app, then the Glitch terminal. Remember, your browser has no terminal - it has a console, but no terminal.

No, but this is completely unrelated to the error. Again, if you are working within Glitch, you are working on someone’s computer, and you need to ensure the packages are installed.

Normally, Glitch automatically installs packages, but sometimes it helps to use the terminal to ensure everything is as it should be.

Finally, be aware of typos. Here is a hint:

There are two issues with the above code. I suggest taking a look at previous lessons on the topic of routes in express.

Otherwise, if you are still stuck, be sure to ask specific questions.

Hope this helps

My apologies, both of those issues were troubleshooting attempts, which I forgot to delete. I have switched the code in question to:

app.post('api/users', (req,res) => {

And I still get the same result. Also, my mongoose.connection.readyState still gives me a ‘2’ on the console, which means ‘connecting’, not a ‘1’, which means ‘connected’.

This is still not correct. Have a look at the very first route.

If you are getting a connection issue with Mongoose, it is almost certainly an issue with your URI being incorrect. Or, if you did not configure your Atlas account to whitelist all IPs.

1 Like

Thank you, switched to app.post('/api/users', and now it does log “hello” to the console. Apologies for that.

However, I still feel that there is an issue with my database connection. I opened up my url shortener, link here, which I have not touched since I completed it the other day. Now, on THAT project, I am getting a ‘2’ when I monitor the mongoose connection, instead of a ‘1’.

So, I tried using it. I put in a valid url and got a number generated, as before. But this time, when I put in the number associated with the url, the site no longer redirects to the original url, as it did when the test passed. However, when I look at my ‘Collections’ in the database, I see the url added. And, the url shortener project still passes when I put it into the Solution Link field. So, I’m at a loss.

So, it seems that I had a few different issues, and now I’m down to one question: what are some good methods for troubleshooting my project’s connection to the DB?

You are debugging in an excellent way.

Just so we clear this up:

console.log(mongoose.connection.readyState, (err, data) => {
  if(err) {
    console.log("the error is " + err)
  } else {
    console.log("the data is " + data)

The value of mongoose.connection.readyState will always be 2 here, unless something amazing happens whereby your internet/server connection to MongoDB’s infra is faster than Node can execute the script.

You are using the correct methods - I would not really do any more. However, as I alluded to above, you are working with asynchronous data.

Picture this:

// t - current time
// Node executes script: t = 0us
mongoose.connect(somewhere); // Mongoose starts connecting to your MongoDB DB
// Script reaches this section: t = 40us
const a = mongoose.connection.readyState; // a = 2
// t = 80us
console.log(a); // a is logged to console
// t = 400us
// Mongoose connects to your DB
// t = 800us
// mongoose.connection.readyState == 1

That is to say, the readyState property on mongoose.connection is meant to be used during a request (before trying to access/read/write to the DB) to ensure the connection is still valid from MongoDB’s perspective.

Now that that is clearer, we need to debug if your app is actually connected to your DB or not.

1 Like

OK makes perfect sense, the code executes faster than the DB is accessed, makes sense. I may not have time for this project today, but I will get back asap and I appreciate your feedback and help.

I have been copying the same URI values from the first of 3 projects that have involved a connection to MongoDB, and I’m wondering if the values get changed because I have a free account, or something related to that. I will try going through my .env file and making sure I have current info, and see if I can .save() to the DB, and have a collection show up in the DB, and report back. Thanks!

1 Like