Are the ExerciseTracker tests working properly? I can't pass the "date property" test and neither can the official FCC solution

Hi, I am failing this test in the Exercise Tracker:

The date property of any object in the log array that is returned from GET /api/users/:id/logs should be a string… Use the dateString format of the Date API.

Here’s what I’ve tried:

  • I used the dateString() function to convert my dates to string, before outputting it in JSON. It is stored as Date in Mongo, so I call this dateString() function
  • I ensured my output looks the exact same as the FCC solutions
  • I console logged inputs/outputs to ensure no edge cases are breaking

My code is below, but what confuses me is that the official FCC solution, when submitted as a solution, doesn’t pass the test either. Are other people able to pass it? Thank you.

//Require packages

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

//Connnect to mongo
const mongoURI = process.env['DATABASE'];
mongoose.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true });
var User = require('./models/userModel.js');
var Exercise = require('./models/exercise.js');

//Middleware

app.use(cors())
app.use(express.static('public'))
app.use(bodyParser.urlencoded({
    extended: true
}));

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/views/index.html')
});

app.post('/api/users', (req, res) => {
  console.log("route: users post: " + req.body.username);
  var username = req.body.username; 
  var obj = new User( {username: username} );
  obj.save(function(err, obj){
       if(err){
         return console.error(err)} else{
             res.json({ username: username, _id: obj._id });
       }
    } 
  )
});

app.post('/api/users/:id/exercises', (req, res) => {
  console.log("route: post exercises. testing id: "+ req.params.id + ".  inputted date: " + req.body.date);
 
  var id = req.params.id;
  var description = req.body.description;
  var duration = req.body.duration;
  var dateInput = req.body.date;

  var date;
  var regex = /([0-9]{4})-([0-9]{2})-([0-9]{2})/;  

  if(dateInput == "" || dateInput == undefined){
      date = new Date();
  } else if(dateInput.match(regex)){
      let result = dateInput.match(regex);
    //Month-1 since months are 0-indexed
      date = new Date(result[1],result[2]-1,result[3]);
  } else{
    //For any other cases, like string, just send it to Mongo and let it trigger validation error
    date = req.body.date;
  }

  var obj = new Exercise({ id: id, description: description, duration: duration, date: date });

  var username; 
  User.findById(id, (err, obj) =>{
    if(err) return console.error(err);
    if(!obj) res.json({error: "Can't find user"});
    username = obj.username;
  });

  obj.save(function(err, obj){
       if(err){
         return res.send(err.name + ": " + err.message)} else{
             res.json({ _id: id, username: username, date: date.toDateString(), duration: parseInt(duration), description: description });
       }
  });

});

app.get('/api/users', (req, res) => {
  console.log("route: users get all");
  User.find({}, function (err, obj) {
    res.json( obj );
  })
});

app.get('/api/users/:id/logs', (req, res) => {
  console.log("route: user logs. user id: " + req.params.id);
  //620698d725f23b55671eb7a6  is my test user ID
  var id = req.params.id;
  var username;
  var temp = [];

  var userQuery = User.findById(id);
  var exerciseQuery = Exercise.find({ id:id }).select('description duration date -_id')

  if(req.query.limit){
    exerciseQuery = exerciseQuery.limit(req.query.limit);
  }

  if(req.query.from){
    var dateInput = req.query.from;
    var regex = /([0-9]{4})-([0-9]{2})-([0-9]{2})/;  

    if(dateInput.match(regex)){
      let result = dateInput.match(regex);  
      var fromDate = new Date(result[1],result[2]-1,result[3]);
      exerciseQuery = exerciseQuery.where('date').gte(fromDate);
    } else{
      res.json({error: "Date not valid! Try again."});
    }
  }

  if(req.query.to){
    var dateInput = req.query.to;
    var regex = /([0-9]{4})-([0-9]{2})-([0-9]{2})/;  

    if(dateInput.match(regex)){
      let result = dateInput.match(regex);  
      var toDate = new Date(result[1],result[2]-1,result[3]);
      exerciseQuery = exerciseQuery.where('date').lte(toDate);
    } else{
      res.json({error: "Date not valid! Try again."});
    }
  }

Promise.all([
  userQuery,
  exerciseQuery
])
  .then(results => {
    const[user, exercises] = results;
    exercises.forEach(item => {
      temp.push({description: item.description, duration: item.duration, date: item.date.toDateString()})
      console.log(item.date.toDateString());
      console.log(typeof item.date.toDateString());
    });
     res.json( { _id: id, username: user.username, count: exercises.length, log: temp } );
  })
  .catch( err => {
    res.json({error: "Something didn't work correctly!"});
  }
);
});

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

Check the forums for the many mentions of the time zone offset problem with this challenge and retry the tests when your local date and the UTC date are the same.

1 Like

I’ll give that a shot, thank you friend.

I found a post @jeremy.a.gray made last year (link) and it was correct about time zone offset being the issue. All I had to do was submit my project earlier in the day (while my local time zone and UTC time zone matches up) and it passed without a problem. Hope his post helps anyone in the future experiencing the same issue.

Thank you to Jeremy!