Hello,
In the last tasks of the exercise-tracker assignment we have to utilise “/log” to find the history of activity for specific users. It’s been almost a month and I am still struggling. My code returns exactly what I want it to return and meets all the requirements of the exercise but the automatic checker of freecode camp still marks me as wrong for the last 5 questions (oddly enough not the very last one). Can someone help me spot the problem? and if possible potentially reccommend a solution as well.
Here is the code:
const express = require('express')
const app = express()
const cors = require('cors')
const mongoose = require("mongoose")
const {Schema} = mongoose;
const bodyParser = require("body-parser")
require('dotenv').config()
app.use(cors())
app.use(express.static('public'))
app.use(bodyParser.urlencoded({ extended: false}));
app.use(bodyParser.json())
const MONGO_URI = process.env.MONGO_URI;
mongoose.connect(MONGO_URI)
const personSchema = new Schema({
"username": { type: String, required: true}
})
const exerciseSchema = new Schema({
"userId": {type: String, required:true},
"description": {type: String, required: true},
"duration": {type: Number, required: true},
"date": {type: Date, required: true},
})
const Person = mongoose.model("Person", personSchema)
const Exercises = mongoose.model("Exercises", exerciseSchema)
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
app.post("/api/users", (req, res) =>{
const username = req.body.username
const createPerson = new Person({ "username": username})
createPerson.save((error, data) =>{
if(error){
res.json(error)
} else{
res.json(data)
}
})
})
app.get("/api/users", (req, res) =>{
Person.find({}, (error, data)=>{
if(error){
res.json(error)
} else{
res.json(data)
}
})
})
app.post("/api/users/:_id/exercises", (req, res) => {
const id = req.params._id
const {description, duration, date} = req.body
Person.findById(id, (error, usrData) => {
if(error) {
res.json(error);
}else{
const newExercises = new Exercises({
userId: id,
description,
duration,
date: new Date(date),
})
newExercises.save((error, data) => {
if(error) {
res.json(error)
}else {
const { description, duration, date, _id} = data;
res.json({
username: usrData.username,
description,
duration,
date: date.toDateString(),
_id: usrData.id
})
}
})
}
})
})
app.get("/api/users", (req, res) =>{
Person.find({}, (error, data)=>{
if(error){
res.json(error)
} else{
res.json(data)
}
})
})
app.get("/api/users/:_id/logs", (req, res) => {
const { from, to, limit } = req.query;
const id = req.params._id;
let dateObj = {}
let usrFilter = {"userId": id}
let limitReach = limit ?? 500
Person.findById(id, (error, usrData) => {
if(error) {
res.json(error);
}else{
if(from){
dateObj["$gte"] = new Date(from)
}
if(to){
dateObj["$lte"] = new Date(to)
}
if(from || to ){
usrFilter.date = dateObj
}
if(null){
newDate = new Date()
}
Exercises.find(usrFilter).limit(+limitReach).exec((error, info) => {
if(error){
res.json(error)
}else{
const count = info.length
const {username, _id} = usrData;
const log = info.map((l) => ({
description:l.description,
duration:l.duration,
date:l.date.toDateString()
}))
res.json({username, count, _id, log})
}
})
}
})
})
app.get("/mongo-health", (req, res) =>{
res.json({status: mongoose.connection.readyState})
})
const listener = app.listen(process.env.PORT || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port)
})