Hello,
Greetings,
My code gives me the correct output for the last " from , to ,limit" test which I separately verified with “https://exercise-tracker.freecodecamp.rocks/ " however, for some reason it keeps failing with " Test timed out error” when freeCodeCamp runs its last test.
My only issue is with this last test bit:
/////
" You can add from
, to
and limit
parameters to a GET /api/users/:_id/logs
request to retrieve part of the log of any user. from
and to
are dates in yyyy-mm-dd
format. limit
is an integer of how many logs to send back."
/////
I’m new to coding and would appreciated greatly if somebody can point out where the error is. This has been bugging me for days now.
Please find below my code as typed in Replit: ( You can scroll to the GET logs section where I believe the issue may lie if any.)
const express = require('express')
const app = express()
const cors = require('cors')
require('dotenv').config()
const bodyParser = require('body-parser')
const mongoose = require("mongoose");
const { Schema } = require("mongoose");
const exercise = {
description: { type: String, required: true },
duration: { type: Number, required: true },
date: { type: String, required: true }
}
const personSchema = new Schema({
username: {
type: String,
required: true
},
count: Number,
exercise: {
type: Array,
value: exercise
}
});
const Person = mongoose.model('Person', personSchema);
const URL = process.env.MONGO_URI;
mongoose.connect(URL);
app.use(cors())
app.use(express.static('public'))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
app.use(bodyParser.urlencoded({ extended: false }));
app.route('/api/users')
.post((req, res) => {
let a = req.body.username;
let person = new Person({ username: a, count: 0 });
person.save(function(err, data) {
res.json({ "username": data.username, "_id": data._id });
})
})
.get((req, res) => {
Person.find((err, data) =>
res.json(data))
});
app.post('/api/users/:_id/exercises', (req, res) => {
Person.findById(req.params._id, (err, data) => {
let datt = '';
// console.log(req.body.date, typeof req.body.date)
if (typeof req.body.date == 'undefined' ||
typeof req.body.date == '') {
datt = (new Date()).toDateString();
}
else {
datt = (new Date(req.body.date)).toDateString();}
exercise.description = req.body.description;
exercise.duration = parseInt(req.body.duration);
exercise.date = datt;
data.count = data.count + 1;
data.exercise.unshift(exercise);
data.save();
res.send({
"_id": req.params._id,
"username": data.username,
'date': exercise.date,
'duration': exercise.duration,
'description': exercise.description
} ); }); });
app.get('/api/users/:_id/logs', (req, res) => {
//console.log(req.params, req.query, req.path);
Person.findById(req.params._id, (err, data) => {
// console.log(data)
let limit = data.exercise.length;
let from = '1970-01-01';
let to = '2038-01-18';
if (typeof req.query.from != 'undefined')
{from = req.query.from;
}
if (typeof req.query.to != 'undefined')
{ to = req.query.to;
}
if (typeof req.query.limit != 'undefined')
{ limit = parseInt(req.query.limit);
}
let fromF = new Date(from).toDateString();
let toF = new Date(to).toDateString();
let ans = data.exercise.filter( q =>
new Date(q.date).getTime() >= new Date(from).getTime() &&
new Date(q.date).getTime() < new Date(to).getTime()
);
let resp = {
"_id" : req.params._id,
"username": data.username
};
if (from != '1970-01-01') resp.from = fromF;
if (to != '2038-01-18') resp.to = toF;
resp.count= ans.slice(0, limit).length;
resp.log = ans.slice(0, limit);
res.json(resp);
if (typeof req.query.from != 'undefined' ||
typeof req.query.to != 'undefined'||
typeof req.query.limit != 'undefined')
{console.log(req._parsedOriginalUrl.path);
console.log(resp); } }); });
const listener = app.listen(process.env.PORT || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port)
})
Here’s my Replit link: https://replit.com/@VarunDevS/boilerplate-project-exercisetracker#index.js
Thanks!!
UPDATE:
I have tried using a different system with higher internet speeds but it did not make any difference.
After hours of struggling, I have finally set up my local server and ran the code locally on Visual Studio Code which has given very strange results.
Sometimes all the tests pass.
It just appears to randomly pass and fail tests - especially the logs related tests.
I have tried clearing my Mongo Database, which occasionally results in all the tests passing.