Dates for getLogs are incorrect, but they look correct

Tell us what’s happening:
I can’t pass the test case for the date property of GET’ing logs. However, when I manually enter tests of my own and compare it with the example application, everything matches up correctly.

Your project link(s)

solution: boilerplate-project-exercisetracker-1 - Replit

const express = require(‘express’)
const app = express()
const cors = require(‘cors’)
require(‘dotenv’).config()
const mongoose = require(‘mongoose’);
const bodyParser = require(“body-parser”);
const { Schema } = mongoose;

app.use(bodyParser.urlencoded({ extended: “false” }));
app.use(bodyParser.json());

app.use(cors())
app.use(express.static(‘public’))
app.get(’/’, (req, res) => {
res.sendFile(__dirname + ‘/views/index.html’)
});

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

const userSchema = new Schema({
username : String
});

const exerciseSchema = new Schema({
userId: String,
username : String,
description: String,
duration: Number,
date: Date
});

let User = mongoose.model(‘User’, userSchema);
let Exercise = mongoose.model(‘Exercise’, exerciseSchema);

const addUser = (username, done) => {
const user = new User({
username : username
});
user.save(function(err, data) {
if (err) return console.error(err);
done(null, { username: data.username, _id: data._id });
});
};

const getUsers = (done) => {
User.find(function(err, data) {
if (err) return console.error(err);
done(null, data);
});
};

const addExercise = (id, description, duration, date, done) => {
let exerciseDate = (!date) ? new Date() : new Date(date);

User.findById({ _id: id}, function(err, user) {
if (err) return console.error(err);
const exercise = new Exercise({
username : user.username,
userId : id,
description : description,
duration : duration,
date : exerciseDate
});
exercise.save(function(err, data) {
if (err) return console.error(err);
let dateString = data.date.toDateString();
done(null, {
_id : data.userId,
username : data.username,
date : dateString,
duration : data.duration,
description : data.description
});
});
});
};

const getLogs = (id, from, to, limit, done) => {
Exercise.find({ userId: id }, function(err, exercises) {
if (err) return console.error(err);
const count = exercises.length;
const username = exercises[0].username;
let modifiedLogs = exercises.filter((exercise) => {
if(from && to) {
let startDate = new Date(from);
let endDate = new Date(to);
return (exercise.date >= startDate && exercise.date <= endDate);
} else if(from) {
let startDate = new Date(from);
return (exercise.date >= startDate);
} else if(to) {
let endDate = new Date(to);
return (exercise.date <= endDate);
} else {
return true;
}
}).map((exercise) => {
let dateString = exercise.date.toDateString();
return {
description: exercise.description,
duration: exercise.duration,
date: dateString
};
});
if (limit) {
modifiedLogs = modifiedLogs.slice(limit);
}
done(null, {
_id : id,
username : username,
count : count,
log : modifiedLogs
});
});
};

app.post(’/api/users’, function(req, res) {
addUser(req.body.username, function (err, data) {
if(err) return console.error(err);
res.json(data);
});
});

app.get(’/api/users’, function(req, res) {
getUsers(function (err, data) {
if(err) return console.error(err);
res.json(data);
});
});

app.post(’/api/users/:_id/exercises’, function(req, res) {
console.log(“Post Exercise”);
console.log("Id: ", req.params._id);
console.log("Description: ", req.body.description);
console.log("Duration: ", req.body.duration);
console.log("Date: ", req.body.date);
addExercise(req.params._id, req.body.description, req.body.duration, req.body.date, function (err, data) {
if(err) return console.error(err);
res.json(data);
});
});

app.get(’/api/users/:_id/logs’, function(req, res) {
console.log(“Get Logs”);
console.log("Id: ", req.params._id);
console.log("From: ", req.params.from);
console.log("To: ", req.params.to);
console.log("Limit: ", req.params.limit);
getLogs(req.params._id, req.query.from, req.query.to, req.query.limit, function (err, data) {
if(err) return console.error(err);
console.log(data);
res.json(data);
});
});

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

Your browser information:

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

Challenge: Exercise Tracker

Link to the challenge:

For some reason after uncommenting some logging statements, it started to work.