Exercise Tracker Project Last Challenge Not Passed

I’m pretty sure my output looks exactly like the example and I keep testing back and forth but for some reason the camperbot just doesn’t approve it. I feel really bad having to put up a thread for this and I do appreciate anybody with his/her spare time because I value that a lot.

To make things simple:

const express = require('express');
const app = express();
const cors = require('cors');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');

app.use(cors(), bodyParser.urlencoded({extended: false}));
app.get('/', (req, res) => {
  res.sendFile(__dirname + '/views/index.html')

mongoose.connect(process.env.DB_URI, { useNewUrlParser: true, useUnifiedTopology: true });

const userSchema = new mongoose.Schema({
  username: String
const User = mongoose.model('User', userSchema);
unwrap = function({_id, username}) {
  return {_id, username};

const exerciseSchema = new mongoose.Schema({
  username: String,
  userId: String,
  description: String,
  duration: Number,
  date: Date
const Exercise = mongoose.model('Exercise', exerciseSchema);

exerciseUnwrap = function({username, userId, description, duration, date}) {
  return {username, userId, description, duration, date};

app.post('/api/exercise/new-user', function(req, res) {
  const newUser = new User({username: req.body.username});
  newUser.save(function(err, data) {
    if (err) console.log('error creating new user');

app.get('/api/exercise/users', function(req, res) {
  User.find(function(err, data) {
    if (err) console.log("cannot retrieve data");
    else {

app.post('/api/exercise/add', function(req, res) {
  User.findOne({_id: req.body.userId}, function(err, userInstance) {
    if (err) console.log('cannot find user');
    else {
      const newExercise = new Exercise({
        username: userInstance.username,
        userId: req.body.userId,
        description: req.body.description,
        duration: req.body.duration,
        date: req.body.date || Date()
      newExercise.save(function(err, data) {
        if (err) {
          console.log('error creating new exercise');
      const output = {
        _id: userInstance._id,
        username: userInstance.username,
        date: newExercise.date.toDateString(),
        duration: newExercise.duration,
        description: newExercise.description
      // console.log(output);

app.get('/api/exercise/log', function(req, res) {

    User.findOne({_id: req.query.userId}, function(err, user) {

        if (err) console.log('error retrieving user');

        const options = {};

        if (req.query.limit) {
        options.limit = req.query.limit

        const query = {userId: req.query.userId};

        if (req.query.from) {
        if (query.date) query.date.$gte = (new Date(req.query.from)).toDateString();
        else query.date = {$gte : (new Date(req.query.from)).toDateString()};

        if (req.query.to) {
        if (query.date) query.date.$lte = (new Date(req.query.to)).toDateString();
        else query.date = {$lte : (new Date(req.query.to)).toDateString()}

        Exercise.find({userId: req.query.userId}, options).sort({'date': 'desc'}).select('-_id description duration date').exec(function(err, data) {

            if (err) console.log('error retrieving activities');

            else {
                const output = {
                    _id: user._id,
                    username: user.username
                if (query.date) {
                    if (query.date.$gte) output.from = query.date.$gte;
                    if (query.date.$lte) output.to = query.date.$lte;
                output.count = data.length;
                output.log = data.map((e) => {
                        return {
                            description: e.description,
                            duration: e.duration,
                            date: e.date.toDateString()


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

Wish you all a great week ahead. For my debugging hours never ending.

Hello there,

Just to start, here are the tests: freeCodeCamp/exercise-tracker.md at master · freeCodeCamp/freeCodeCamp (github.com)

Otherwise, a common pitfall occurs when campers have some logic causing multiple entries to the same log. That is, a single request to /add, but multiple exercises are added to log.

Also, you might need to make sure you are searching the DB using .toISOString, and converting back to toDateString() when responding.

Hope this helps you on your debugging journey.

1 Like

Thank you man, damn you’re like an angel and you appear everywhere I go lol.
I’ll check out the tests shortly. So handy to have them in my hands ngl.

1 Like