Back End Development and APIs Projects - Exercise Tracker

Tell us what’s happening:
Cant seem to pass the last test please help stuck for a long time

Your browser information:

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

Challenge: Back End Development and APIs Projects - Exercise Tracker

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

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

//mongoose
const mongodb = require('mongodb');
const mongoose = require('mongoose');
const mySecret = process.env['MONGO_URL'];
const { Schema } = mongoose;

//mongoose schema
const exerciseSchema = new Schema({
  username: String,
  description: String,
  duration: Number,
  date: String,
  userId: String
})

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

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

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

let count = 0;

app.use(bodyParser.urlencoded({extended: false}))

// User.deleteMany({}).exec()
// Exercise.deleteMany({}).exec()

//POST to Users

app.post('/api/users', async(req,res) => {

  try{

    const userName = await req.body.username;
    count++;

    let newUser = new User({
      username: userName
      // userId: count
    })

    let myUser = await User.findOne({username: userName})

    if(!myUser){
      await User.create(newUser)

      myUser = await User.findOne({username: userName})

      res.json({
        username: userName,
        _id: myUser._id
      })
    }
    else{
      res.json({error: `User already exists with an Id of ${myUser._id}`})
    }
  }
  catch(err){
    console.log(err)
  }
})

//GET from Users

app.get('/api/users', async(req,res) => {
  try{

    await User.find({}, (err,data) => {

      let usersMap = []

      data.forEach(d => usersMap.push({
        username: d.username,
        _id: d._id
      }))

      res.send(usersMap)
    })
  }
  catch(err){
    console.log(err)
  }
})

//POST to exercises

app.post('/api/users/:_id/exercises', async(req,res) => {
  try{

    const _id = await req.params;
    const description = await req.body.description;
    const duration = await req.body.duration;
    let date = await req.body.date;
    let currDate = '';
    
    if (!date) {

      currDate = new Date(Date.now());

      let myUser = await User.find({_id: _id})
      
      let newExercise = new Exercise({
        username: myUser[0]._doc.username,
        description: description,
        duration: duration,
        date: currDate.toISOString(),
        userId: myUser[0]._doc._id
      })
      
      await Exercise.create(newExercise)

      res.json({
        username: myUser[0]._doc.username,
        description: description,
        duration: parseInt(duration),
        date: currDate.toDateString(),
        _id: myUser[0]._doc._id
      })
    }
    else {

      currDate = date;

      if (currDate.match(/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/)) {

        let tempDate = currDate.split('-')
        tempDate = new Date(tempDate[0],tempDate[1]-1,tempDate[2])

        let myUser = await User.find({_id: _id})

        let newExercise = new Exercise({
          username: myUser[0]._doc.username,
          description: description,
          duration: duration,
          date: tempDate.toISOString(),
          userId: myUser[0]._doc._id
        })

        await Exercise.create(newExercise)

        res.json({
          username: myUser[0]._doc.username,
          description: description,
          duration: parseInt(duration),
          date: tempDate.toDateString(),
          _id: myUser[0]._doc._id
        })
      }
      else {
        res.json({error: "Invalid Date Format"})
      }
    }
  }
  catch(err){
    console.log(err)
  }
})

app.get('/api/users/:_id/logs', async(req,res) => {
  try{
    const { from } = await req.query;
    const { to } = await req.query;
    const { limit } = await req.query;
    const _id = await req.params;

    let myUser = await User.find({_id: _id})

    let dateObj = {}

    if(from){
      dateObj["$gte"] = new Date(from).toISOString()
    }
    if(to){
      dateObj["$lte"] = new Date(to).toISOString()
    }

    let filter = {
      userId: myUser[0]._doc._id
    }
    if (from || to){
      filter.date = dateObj
    }

    let myExercise = {}
    if(limit){
      myExercise = await Exercise.find(filter).limit(parseInt(limit))
    }
    else{
      myExercise = await Exercise.find(filter)
    }

    let myLog = []
    myExercise.map(d => myLog.push({
        description: d.description,
        duration: d.duration,
        date: new Date(d.date.split('T')[0].split('-')[0],d.date.split('T')[0].split('-')[1]-1,d.date.split('T')[0].split('-')[2]).toDateString()
      }))

    res.json({
      username: myUser[0]._doc.username,
      count: myExercise.length,
      _id: myUser[0]._doc._id,
      log: myLog
    })
  }
  catch(err){
    console.log(err)
  }
})

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

exports.UserModel = User;
// exports.createAndSaveUser = createAndSaveUse```

**Link to the challenge:**
https://www.freecodecamp.org/learn/back-end-development-and-apis/back-end-development-and-apis-projects/exercise-tracker

Wait really? I have tried more than 5 times but the last test always seem to fail

i see thank you so much for helping me i will keep trying

Tell us what’s happening:
Cant pass the last test HELP

https://replit.com/@SohaibIrfan/exercisetracker?v=1
code^

const express = require('express')
const app = express()
const cors = require('cors')
const moment = require('moment')
const bodyParser = require("body-parser")
require('dotenv').config()

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

//mongoose
const mongodb = require('mongodb');
const mongoose = require('mongoose');
const mySecret = process.env['MONGO_URL'];
const { Schema } = mongoose;

//mongoose schema
const exerciseSchema = new Schema({
  username: String,
  description: String,
  duration: Number,
  date: String,
  userId: String
})

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

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

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

let count = 0;

app.use(bodyParser.urlencoded({extended: false}))

// User.deleteMany({}).exec()
// Exercise.deleteMany({}).exec()

//POST to Users

app.post('/api/users', async(req,res) => {

  try{

    const userName = await req.body.username;
    count++;

    let newUser = new User({
      username: userName
      // userId: count
    })

    let myUser = await User.findOne({username: userName})

    if(!myUser){
      await User.create(newUser)

      myUser = await User.findOne({username: userName})

      res.json({
        username: userName,
        _id: myUser._id
      })
    }
    else{
      res.json({error: `User already exists with an Id of ${myUser._id}`})
    }
  }
  catch(err){
    console.log(err)
  }
})

//GET from Users

app.get('/api/users', async(req,res) => {
  try{

    await User.find({}, (err,data) => {

      let usersMap = []

      data.forEach(d => usersMap.push({
        username: d.username,
        _id: d._id
      }))

      res.send(usersMap)
    })
  }
  catch(err){
    console.log(err)
  }
})

//POST to exercises

app.post('/api/users/:_id/exercises', async(req,res) => {
  try{

    const _id = await req.params;
    const description = await req.body.description;
    const duration = await req.body.duration;
    let date = await req.body.date;
    let currDate = '';
    
    if (!date) {

      currDate = new Date(Date.now());

      let myUser = await User.find({_id: _id})
      
      let newExercise = new Exercise({
        username: myUser[0]._doc.username,
        description: description,
        duration: duration,
        date: currDate.toISOString(),
        userId: myUser[0]._doc._id
      })
      
      await Exercise.create(newExercise)

      res.json({
        username: myUser[0]._doc.username,
        description: description,
        duration: parseInt(duration),
        date: currDate.toDateString(),
        _id: myUser[0]._doc._id
      })
    }
    else {

      currDate = date;

      if (currDate.match(/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/)) {

        let tempDate = currDate.split('-')
        tempDate = new Date(tempDate[0],tempDate[1]-1,tempDate[2])

        let myUser = await User.find({_id: _id})

        let newExercise = new Exercise({
          username: myUser[0]._doc.username,
          description: description,
          duration: duration,
          date: tempDate.toISOString(),
          userId: myUser[0]._doc._id
        })

        await Exercise.create(newExercise)

        res.json({
          username: myUser[0]._doc.username,
          description: description,
          duration: parseInt(duration),
          date: tempDate.toDateString(),
          _id: myUser[0]._doc._id
        })
      }
      else {
        res.json({error: "Invalid Date Format"})
      }
    }
  }
  catch(err){
    console.log(err)
  }
})

app.get('/api/users/:_id/logs', async(req,res) => {
  try{
    const { from } = await req.query;
    const { to } = await req.query;
    const { limit } = await req.query;
    const _id = await req.params;

    let myUser = await User.find({_id: _id})

    let dateObj = {}

    if(from){
      dateObj["$gte"] = new Date(from).toISOString()
    }
    if(to){
      dateObj["$lte"] = new Date(to).toISOString()
    }

    let filter = {
      userId: myUser[0]._doc._id
    }
    if (from || to){
      filter.date = dateObj
    }

    let myExercise = {}
    if(limit){
      myExercise = await Exercise.find(filter).limit(parseInt(limit))
    }
    else{
      myExercise = await Exercise.find(filter)
    }

    let myLog = []
    myExercise.map(d => myLog.push({
        description: d.description,
        duration: d.duration,
        date: new Date(d.date.split('T')[0].split('-')[0],d.date.split('T')[0].split('-')[1]-1,d.date.split('T')[0].split('-')[2]).toDateString()
      }))

    res.json({
      username: myUser[0]._doc.username,
      count: myExercise.length,
      _id: myUser[0]._doc._id,
      log: myLog
    })
  }
  catch(err){
    console.log(err)
  }
})

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

exports.UserModel = User;
// exports.createAndSaveUser = createAndSaveUser;

Your project link(s)

Your browser information:

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

Challenge: Back End Development and APIs Projects - Exercise Tracker

I have merged your threads. Please do not create duplicate topics for the same challenge/project question(s).

Thank you.

Hey I solved it. I just changed the Mongo Atlas region to USA Thought it might help and it did :).

Interesting, what region did you have selected before?

I haven’t had any issues with the EU regions. I think I usually pick Germany (or Sweden if available).

Well I selected India since I live in India but that didn’t seem to work well for me so I guessed that FCC servers might be in NA so what would happen if I changed atlas servers to USA , and it worked after 6 hours of hopelessness

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.