I need help in passing one task in Exercise tracker. It's too frustrating please I need help!

**Hello guys. I’m having problem passing this test below :

" You can make a GET request to /api/exercise/log with a parameter of userId=_id to retrieve a full exercise log of any user. The returned response will be the user object with a log array of all the exercises added. Each log item has the description , duration , and date properties."

The test work on my local computer and on repl.it, but when i pass it, i don’t accept it as the correct code. Can someone please help me?
**

**Your code so far:

app.get('/api/exercise/log', (req, res) => {
   let { userId } = req.query;

  exerciseModel.findOne({_id: userId }, (err, person)=>{
    if(err) return err;
    if(person) {
      const log = [];
      exerciseModel.find().then(data => {
        data.forEach(item => {
          log.push(item.exercise[0])
         });
        res.json({
          _id: person._id,
          username: person.username,
          count: log.length,
          log: log
        });
      })
    }
  })

});

**

This is the link to my repl.it exercise tracker: https://repl.it/@Micode111/boilerplate-project-exercisetracker#server.js

Your browser information:

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

Challenge: Exercise Tracker

Link to the challenge:

Without testing your code, I would be concerned here:

as the find() you have should find every record in that collection, regardless of user id. Regardless, I would build the response object separate from the res.json() call and console.log() the object to see what it contains, or at the very least, console.log() the log array to see what it contains and then run the fCC tests against your project to inspect the output.

Once you get that working, then you can implement the filters on from, to, and limit. Good luck.

1 Like

I did that and still it’s return was an error message stating that “description is undefined” I wrote the object in the response.json call because i was trying to simplify the code forgetting about the rule about protecting data from attackers, that was my bad. But if this method is wrong what is the best approach in order to secure data in terms of using the find() method?

The “description is undefined” is telling you that your description field of your exercise record is not there. So it is either not being stored in the collection or it is not being found. I had envisioned something like

    if(person) {
      const log = [];
      exerciseModel.find().then(data => {
        data.forEach(item => {
          log.push(item.exercise[0])
         });
        console.log(log);
        let data = {
          _id: person._id,
          username: person.username,
          count: log.length,
          log: log
        };
        console.log(data);
        return res.json(data);
      });
    }

so you can see exactly what is being found and sent back.

You can use several different search functions, but most need a field. So you might want to use find({"username": person.username}) or findById(userId) (check the mongoose docs for details and more options). Regardless, just log the records and adjust until you are certain you are finding the correct records.

Ok. I have solved it. The problem was a misunderstanding of what freecodecamp wanted me to do. First they stated:

You can make a GET request to /api/exercise/log with a parameter of userId=_id to retrieve “A FULL EXERCISE OF " ANY USER”

So i thought they meant all the exercises of users in the database that’s why i used mongo.find() but what i was meant to do is to get the EXERCISE of ONE USER (i.e the user with the “userId” that im searching for in the query). Plus, in my task 4 of adding users, i was meant to push exercises into the users array. But i just kept getting one exercise because i was only updating the data in my mongo DB database. I’m talking about this task:

You can POST to /api/exercise/add with form data userId=_id , description , duration , and optionally date . If no date is supplied, the current date will be used. The response returned will be the user object with the exercise fields added.

So now I reworked on the code on task 4 and I am now pushing more than one exercise data into one user’s exercise array, that’s what was done in freecodecamp’s example . So here is the code now to answer my question above which is TASK 5:

app.get('/api/exercise/log', (req, res) => {
  let { userId } = req.query;

exerciseModel.findOne({_id: userId }, (err, person) => {
  if(err) return err;
  if(person) {
    let userData = {
      _id: person._id,
      username: person.username,
      count: person.exercise.length,
      log: person.exercise
    }

    res.json(userData);
  }
})
});

Now time to solve my last task. :slightly_smiling_face: