# API & Microservices Projects - Exercise Tracker

**URL:** <https://forum.freecodecamp.org/t/api-microservices-projects-exercise-tracker/378267>\
**Category:** General\
**Created:** [April 24, 2020, 1:31pm UTC](https://forum.freecodecamp.org/t/api-microservices-projects-exercise-tracker/378267 "2020-04-24T13:31:26Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bshaddie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bshaddie/32/137699_2.png) [@Bshaddie](https://forum.freecodecamp.org/u/Bshaddie)\
**Post date:** [April 24, 2020, 1:31pm UTC](https://forum.freecodecamp.org/t/api-microservices-projects-exercise-tracker/378267/1 "2020-04-24T13:31:26Z")

</div>

Hey there! I have been parsuing the exercise tracker challenge and am stuck. Tried all there is but I keep failing 3 tests. Can someone kindly peruse my code and guide me please. Thanks  
My code:  
[https://chipped-melon-seaplane.glitch.me](https://chipped-melon-seaplane.glitch.me)

---

<div class="post-metadata">

**Author:** ![LuosRestil](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/luosrestil/32/130284_2.png) [@LuosRestil](https://forum.freecodecamp.org/u/LuosRestil)\
**Post date:** [April 24, 2020, 5:53pm UTC](https://forum.freecodecamp.org/t/api-microservices-projects-exercise-tracker/378267/2 "2020-04-24T17:53:40Z")

</div>

I think you may have forgotten a link to your code. [edit: Link now posted.]

---

<div class="post-metadata">

**Author:** ![Bshaddie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bshaddie/32/137699_2.png) [@Bshaddie](https://forum.freecodecamp.org/u/Bshaddie)\
**Post date:** [April 24, 2020, 5:57pm UTC](https://forum.freecodecamp.org/t/api-microservices-projects-exercise-tracker/378267/3 "2020-04-24T17:57:29Z")

</div>

@LuosRestil Guide me please. What link???

---

<div class="post-metadata">

**Author:** ![krauss](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/krauss/32/129757_2.png) [@krauss](https://forum.freecodecamp.org/u/krauss)\
**Post date:** [April 24, 2020, 6:25pm UTC](https://forum.freecodecamp.org/t/api-microservices-projects-exercise-tracker/378267/4 "2020-04-24T18:25:26Z")

</div>

Hey @Bshaddie, I had a quick look at your code and it seems to me that you’re missing the part where you were supposed to persist your objects into your MongoDB database through Mongoose.

```javascript

app.post("/api/exercise/new-user", (req, res) => {
  const { username } = req.body;

  const newUser = {
    username,
    _id: shortid.generate()
  };

  /*
  'users' is an array. Pushing newUser object into an array  
  won't save it into your MongoDB database.
  */
  users.push(newUser);

  return res.json(newUser);
});

app.get("/api/exercise/users", (req, res) => {
  return res.json(users);
});

app.post("/api/exercise/add", (req, res) => {
  const { userId, description, duration, date } = req.body;
  const dateObj =
    date === "" ? new Date().toDateString() : new Date(date).toDateString;

  const newExercise = {
    username: getUsernameById(userId),
    description,
    duration: parseInt(duration),
    _id: userId,
    date: dateObj
  };
/*
The same situation from above repeats itself here. That 'exercises', is an array. 
*/
  exercises.push(newExercise); 
  res.json(newExercise);
});

```

So you are pushing objects into arrays and use this mongoose method  
`UsernameAndId.findById(searchedUserId, (err, docs))`  
to search for users.

`UsernameAndId` is supposed to be your mongoose model, right? But I couldn’t find neither the `schema` nor the `model` in your code. So I recommend you to start by first defining a Mongoose `schema` for your object and then create a `model` of it.  
Don’t get me wrong, I might sound rude but I’m just trying to help you.

---

<div class="post-metadata">

**Author:** ![Bshaddie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bshaddie/32/137699_2.png) [@Bshaddie](https://forum.freecodecamp.org/u/Bshaddie)\
**Post date:** [April 25, 2020, 9:09am UTC](https://forum.freecodecamp.org/t/api-microservices-projects-exercise-tracker/378267/5 "2020-04-25T09:09:56Z")

</div>

@krauss Not at all. You are not rude! Let me get down to it and see how it goes! Thanks

---

<div class="post-metadata">

**Author:** ![Bshaddie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bshaddie/32/137699_2.png) [@Bshaddie](https://forum.freecodecamp.org/u/Bshaddie)\
**Post date:** [April 25, 2020, 10:12am UTC](https://forum.freecodecamp.org/t/api-microservices-projects-exercise-tracker/378267/6 "2020-04-25T10:12:14Z")

</div>

@krauss I have incorporated the schema and model but I can realise any changes in the test. Still failing the last 3.

---

<div class="post-metadata">

**Author:** ![krauss](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/krauss/32/129757_2.png) [@krauss](https://forum.freecodecamp.org/u/krauss)\
**Post date:** [April 25, 2020, 1:11pm UTC](https://forum.freecodecamp.org/t/api-microservices-projects-exercise-tracker/378267/7 "2020-04-25T13:11:06Z")

</div>

Hey @Bshaddie, I’ve made something comments on your project. I hope that helps and guides you throughout the exercise. Trust me, it’s not that hard. You just really need to get hang of how MongoDB + Mongoose work together. Like I said on one the comments, focus on creating a `newUser` a saving it into your database. That’s the path to accomplish all the other exercise’s requirements.

---

<div class="post-metadata">

**Author:** ![Bshaddie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bshaddie/32/137699_2.png) [@Bshaddie](https://forum.freecodecamp.org/u/Bshaddie)\
**Post date:** [April 25, 2020, 1:20pm UTC](https://forum.freecodecamp.org/t/api-microservices-projects-exercise-tracker/378267/8 "2020-04-25T13:20:08Z")

</div>

@krauss I have seen the comments you made thanx. I have tried to look out for the database\_name but I seem not to locate or know how to find it. I have looked into my MongoDB account but I seem not to find it. Kindly guide me. Thanks

---

<div class="post-metadata">

**Author:** ![krauss](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/krauss/32/129757_2.png) [@krauss](https://forum.freecodecamp.org/u/krauss)\
**Post date:** [April 25, 2020, 1:39pm UTC](https://forum.freecodecamp.org/t/api-microservices-projects-exercise-tracker/378267/9 "2020-04-25T13:39:46Z")

</div>

Don’t forget to check out the [FCC guide](https://www.freecodecamp.org/learn/apis-and-microservices/mongodb-and-mongoose/) on how to create a MongoDB account and login in to the MongoDB Atlas console. I have to admit that when I started **MongoDB and Mongoose** module from **APIs and Microservices Certification** , and I too had skipped the introduction part ✋ 😌 without knowing that it was essential for the success of the all the future projects. So, I went and carefully did it step-by-step. The outcomes of understanding MongoDB and Mongoose were great, it made the 6 last projects so much easier to me. So much so, that I finished my Full Stack Certification two days ago 🙏. So, put all your focus on those guys for now mate, MongoDB and Mongoose. I loved learning about them and I hope you too. P.S: Relational databases never was my thing haha 😅.

---

<div class="post-metadata">

**Author:** ![Bshaddie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bshaddie/32/137699_2.png) [@Bshaddie](https://forum.freecodecamp.org/u/Bshaddie)\
**Post date:** [April 26, 2020, 10:27am UTC](https://forum.freecodecamp.org/t/api-microservices-projects-exercise-tracker/378267/10 "2020-04-26T10:27:08Z")

</div>

@krauss  
I have tried to do the necessary amendments according to the guideline but I still can’t break even with the challenge. Kindly look at my code again and advise accordingly:  
[https://chipped-melon-seaplane.glitch.me](https://chipped-melon-seaplane.glitch.me)  
Thanks

---

<div class="post-metadata">

**Author:** ![krauss](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/krauss/32/129757_2.png) [@krauss](https://forum.freecodecamp.org/u/krauss)\
**Post date:** [April 26, 2020, 2:04pm UTC](https://forum.freecodecamp.org/t/api-microservices-projects-exercise-tracker/378267/11 "2020-04-26T14:04:11Z")

</div>

As you might’ve seen, I’ve done some refactoring in your code just to make it easy for you to grasp the beautiful relationship between MongoDB & Mongoose hahaha.  
From now on, [Mongoose Docs](https://mongoosejs.com/docs/index.html) will be your master guide. Of course, if you still need help, come back here and give us a shout. Just try to be as straight forward as possible with your questions/doubts because we can’t, and shouldn’t, keep coding for you all.  
**If coding is your thing** , have fun and don’t give up. You’ll get there…

---

<div class="post-metadata">

**Author:** ![pauloconnell](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pauloconnell/32/144293_2.png) [@pauloconnell](https://forum.freecodecamp.org/u/pauloconnell)\
**Post date:** [May 5, 2020, 8:33pm UTC](https://forum.freecodecamp.org/t/api-microservices-projects-exercise-tracker/378267/12 "2020-05-05T20:33:55Z")

</div>

Hey B;  
I agree with what krauss said, but I’m going to attempt to drill down into the details a bit to test my own comprehension of it too - the key parts of using the DB is the following:  
mongoose.connect()… to connect to your database, then define your Schema - I’ve called trackerSchema

```auto
const TrackerModel = mongoose.model("TrackerModel", trackerSchema);
console.log(mongoose.connection.readyState);

var tracker=new TrackerModel({username:username};
// remember to put inside an async function:
await tracker.save(err => {
   if(err){ return err;
   }else{ return res.send(tracker)

```

The key bit of code above is that my collection(database) is called “TrackerModel” , trackerSchema defines how documents are stored, and my instance of TrackerModel is called tracker- this instance gets loaded up with data as per the Schema and when you call tracker.save that saves this tracker to the TrackerModels Collection visible in MongDB Atlas…play around on there to see how to view update and delete the DB manually.  
Tricky part here:  
Now if you want to look up data on your collection,(or update/delete) you use TrackerModel.findById or TrackerModel.delete…ect So use tracker to save, but TrackerModel to access/view/update/delete .

Hope that helps?  
Paul

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:55pm UTC](https://forum.freecodecamp.org/t/api-microservices-projects-exercise-tracker/378267/13 "2021-06-01T18:55:37Z")

</div>

Closed on June 1, 2021, 6:55pm UTC


