APIs and Microservices Projects - Exercise Tracker Project

Tell us what’s happening:
Not sure if it is the issue but seems like I have a problem connecting to the databases. I’m doing the “api/exercise/new-user” POST API which should add a new user to the databases if it is not exists, otherwise send a message. Rightnow, when I hit the submit button, it load for a very long time with the “site didn’t respond” message at the end. I just get the code related to the databases and the API function here as there are password for my database account in the process.env variable in the full code.

What I tried so far:

Separate the creating and saving new databases into a new function.
log the newUser - it seems like it still created normally which mean my model is probably set up correctly? But when I call save() on it and check the data on MongoDB Atlas page, it does not have any new model created.
Check the req.body.username - it did capture the correct value.

Your code so far

const exerciseSchema = new Schema ({
  "description": String,
  "duration": Number,
  "date": Date

const userSchema = new Schema ({
  "username": String,
  "count": Number,
log: [exerciseSchema]

let userModel = mongoose.model('userModel', userSchema);
let exerciseModel = mongoose.model('exerciseModel', exerciseSchema);

const createAndSaveUser = (username) => {
  let newUser = new userModel({username: username});

//Create new user
app.post('/api/exercise/new-user', (req,res) => {
  userModel.count({username: req.body.username}, (err,count) => {
    if (err) console.log(err);
    if (count == 0) {
    } else {
      res.send("User already exist.")

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) coc_coc_browser/87.0.152 Chrome/81.0.4044.152 Safari/537.36.

Challenge: Exercise Tracker

Link to the challenge:


I can see a couple of problems…

  1. when you call createAndSaveUser before the count, you’re not waiting for the promise to resolve nor using a callback method. This presents a problem because the user may or may not have been created when you call userModel.count.

  2. Inside the userModel.count callback you’re checking for the error and logging it, which is fine, but you’re not immediately returning. The problem with this is that if there is an error the count variable will be null/undefined, thus will be sending User already exists.

    A better solution for this would be:

    if (err) {
         console.error('Error: failed to count -- ', err);
         return res.status(500).json({ error: 'Could not process your request' });
  3. Inside the count callback you’re making the same mistake as the first: not waiting for the promise to resolve.

  4. Finaly, even if you were using async and await inside the count callback, you’re only responding to the client count is not 0, which is why your application is hanging/timing out.

I tested this on a glitch project and the data is being saved, so there may be other errors on the code you have omitted. You can query the data at /api/users or clicking here.

Try to solve the problems or ask more questions if there’s something you don’t understand :slight_smile:, we’ll try to help you learn.

I hope this helps a little bit,


Hi skaparate,

Thanks for replying to my queries, I figured out the problems was in the boilerplate package.json file FCC supplied.

When I check the mongoose.connection.readyState, it return 0 which means I was disconnected from the db the whole time. The I looked at my log which has the Deprecation Warning everytime the server listening. The mongoose version is 2. in the package.json file at the beginning and I updated to the same version with your glitch project and it works, the db is connected to my project.

The problems was not originally from my code so it makes it quite hard to troubleshoot . I think FCC should update the boilerplate so that other person do not face the same bug as me.


1 Like


I’m happy you solved the problem :slight_smile:!

Nevertheless, I also hope you took into account the problems I mentioned, since those are flaws that you need to take into account in the future :slight_smile:.

I updated my glitch so you can run some tests.


P.D.: You can create a Pull Request on github.com to update the package.json, it should be a good starting point for collaboration :slight_smile:.