Back End Development and APIs Projects - Exercise Tracker

What’s happening:

Hello! I cannot seem to pass the 6th test (with the “new” division of tests) of the Exercise Tracker :

You can POST to /api/users/:_id/exercises with form data description, duration, and optionally date. If no date is supplied, the current date will be used.

All the other tests pass, and the functionality in the live project seems to be what the challenge is asking.

I have seen other posts similar to this but the answers did not help.

The issue is not the timezone one, one more test in my project does not pass while my timezone has a difference with UTC timezone. The 6th test always fails.

So far I have tried to run the tests from :
Different browsers (Chrome, Mozzila, Edge)
Different machines (Desktop, Laptop, Mobile)
Different hosts/servers (Replit, CodeSandBox, Gitpod)
Through proxy

One more thing to note :
I have tried to fork and test solutions of others and they did not work even though the functionality still seemed to be the same.

One probably more important thing to note :
The freeCodeCamp example URL does not pass the test.

My example code :

const express = require('express');
const app = express();
const cors = require('cors');
const mongoose = require('mongoose');
const { Schema } = require('mongoose');
var bodyParser = require('body-parser');
require('dotenv').config();

const DB_CONNECT = process.env['db_full_connection']; 
mongoose.connect(DB_CONNECT);

//user model
const userSchema = new Schema({
  username: {
    type: String,
    required: true
  },
  log: [{
    description: String,
    duration: Number,
    date: String
  }],
  count: {
    type: Number,
    default: 0
  }
});
const User = mongoose.model('User', userSchema);


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


//user create and read
app.route('/api/users')
  .post(function(req, res) {
    const username = req.body.username;
    const user = new User({ username : username });
    user.save(function(err, data){
      if (err) { res.json({ error: err }) }
      res.json(data);
    })
  })
  .get(function(req, res) {
    User.find((err, data) => {
      if (data) {
        res.json(data);
      }
    })
  });


//exercises logging
app.post('/api/users/:_id/exercises', function(req, res) {
  const { description } = req.body;
  const duration = parseInt(req.body.duration);

  const nowDate = new Date();
  //const hackDate = req.body.date !== undefined ? '1990-01-01' : '2022-10-10';
  const rawDate = req.body.date ? 
    new Date(req.body.date).toDateString(): 
    new Date(nowDate.getTime() 
          - nowDate.getTimezoneOffset()*60*1000).toDateString();

  const date = rawDate;
  //const date = rawDate.toDateString();
  //const date = hackDate;
  const id = req.params._id;
  

  const exercise = {
    date,
    duration,
    description  
  };
  
  //add the exercise to the user log
  User.findByIdAndUpdate(id, 
                         { $push: { log : exercise },
                         $inc: { count: 1} },
                         {new: true},
                         function(err, user) {
    if (user) {
      const enteredExercise = {
        _id: id,
        username: user.username,
        ...exercise
      };
      res.json(enteredExercise);
    }
  });
});


//log searching
app.get('/api/users/:_id/logs',function(req, res) {
  const { from, to, limit } = req.query;
  
  if (from && to) {
  const sFrom = new Date(from).toDateString();
  const sTo = new Date(to).toDateString();
  console.log(sFrom, sTo, limit);
  }

  
  User.findById(req.params._id, (req, user) => {
    if (user) {

      if (from || to || limit) {
        let logs = user.log;

        let filteredLogs = [];
        const empty = [];
        //filter from and to
        if (from && to) {
          filteredLogs = logs
          .filter(log => {
            return (log.date <= to) && (log.date >= from);
          });
        }
        
        let afterFiltered = logs;
        if (filteredLogs[0] !== empty[0]) { 
          afterFiltered = filteredLogs;
        } 

        //limit the results
        const slicedLogs = limit ? 
          afterFiltered.slice(0, limit) : 
          afterFiltered;
        user.log = slicedLogs;
      }
      
      res.json(user);
    }
  })
});

//connection testing
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
app.get('/db-health', function(req, res) {
  res.json({ dbStatus: db.readyState });
});

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

Project link(s)

solution: boilerplate-project-exercisetracker - Node.js Repl - Replit

Browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0

Challenge: Back End Development and APIs Projects - Exercise Tracker

Link to the challenge:

Pls see if this is what you are experiencing

1 Like