Is Exercise Tracker running test broken?

I have my code below, and it has the outputs exactly the same like the example of FCC provided… But, unfortunately, the only the first test that is passed. are anyone having solution about this?

const express = require('express')
const app = express()
const cors = require('cors')
const mongoose = require('mongoose')
const { Schema } = require('mongoose')
require('dotenv').config()

app.use(express.urlencoded({ extended: true }))

// MongoDB Database
// mongoose.connect()

const Exercise = mongoose.model('Exercise', {
  username: {
    type: String,
    required: true
  },
  log: [{
    description: String,
    duration: Number,
    date: String,
    _id: false
  }]
})


app.get('/api/users', async (req, res) => {
  const users = await Exercise.find({}, ['username', '__v'])
  res.json(users)
})

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

  limit = limit == undefined ? 1000 : limit
  from = new Date(from) == 'Invalid Date' ? new Date(0) : new Date(from)
  to = new Date(to) == 'Invalid Date' ? new Date() : new Date(to)

  const user = await Exercise.findById(req.params._id)
  if (user == null) return res.json({ error: 'Cannot find the user' })

  const result = { ...user._doc }
  delete result.__v

  const filtered = result.log.filter((element, index) => (index < limit) && (new Date(element.date) > from) && (new Date(element.date) <= to))

  result.log = filtered

  res.json({
    _id: result._id,
    username: result.username,
    count: result.log.length,
    log: result.log
  })
})

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

  const username = await req.body.username
  const test = await Exercise.findOne({ username })

  if (test != null) { console.log('Name already taken') }
  else {
    // Create User
    const user = { username: username }

    const wait = await Exercise.insertMany(user)
    console.log('saved')
  }

  const result = await Exercise.findOne({ username: username })

  res.json({
    username: result.username,
    _id: result._id
  })

})

app.post('/api/users/:_id/exercises', async (req, res) => {
  const id = req.params._id
  const user = await Exercise.findById(id)

  let d = new Date(req.body.date) == 'Invalid Date' ? new Date() : new Date(req.body.date)

  d = d.toDateString()

  const input = {
    description: req.body.description,
    duration: Number(req.body.duration),
    date: d
  }

  user.log.push(input)

  const wait = await Exercise.findByIdAndUpdate(id, user)

  res.json({
    username: user.username,
    description: req.body.description,
    duration: parseInt(req.body.duration),
    date: d,
    _id: user._id
  })
})


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


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

here’s the challenge link
Back End Development and APIs Projects - Exercise Tracker | Learn | freeCodeCamp.org

Posting a repl is much easier for this. I can’t determine if the tests are failing because you don’t have mongoose connected or you just disconnected it to remove your connection details (repl.it does this automagically on a fork if you store the details in keys), though I suspect it’s the latter. If you post a repl and use keys for secrets, then others can just fork and plug in their secrets and work without having to copy/paste and hope everything transferred.

Once I plug in mongoose and add some route input/output logging and run the tests, only the last test fails with this output:

GET /api/users/:_id/logs
req.body: {}
req.params: {"_id":"6213a59e5855e608ce8b42c3"}
req.query: {"from":"1989-12-31","to":"1990-01-04"}
{
  "_id": "6213a59e5855e608ce8b42c3",
  "username": "fcc_test_16454547507",
  "count": 2,
  "log": [
    {
      "description": "test",
      "duration": 60,
      "date": "Mon Jan 01 1990"
    },
    {
      "description": "test",
      "duration": 60,
      "date": "Wed Jan 03 1990"
    }
  ]
}
GET /api/users/:_id/logs
req.body: {}
req.params: {"_id":"6213a59e5855e608ce8b42c3"}
req.query: {"limit":"1"}
{
  "_id": "6213a59e5855e608ce8b42c3",
  "username": "fcc_test_16454547507",
  "count": 1,
  "log": [
    {
      "description": "test",
      "duration": 60,
      "date": "Mon Jan 01 1990"
    }
  ]
}
GET /api/users/:_id/logs
req.body: {}
req.params: {"_id":"6213a59e5855e608ce8b42c3"}
req.query: {"from":"1990-01-02","to":"1990-01-04","limit":"1"}
{
  "_id": "6213a59e5855e608ce8b42c3",
  "username": "fcc_test_16454547507",
  "count": 0,
  "log": []
}

The log entries from the first GET are the records used for this test. The first looks correct; the second may be correct but I can’t remember if you should return from the start or the end with a range and a limit; the last is wrong since the January 3 record is in the range but is not returned. This means your filter isn’t always working, so there is something wrong with your filter condition.

But no, the test is not broken.

yupp, I just find out that is cors the one that caused it, and for the last test, the running test expected for the log to be sorted…

thanks for your respons…

here’s the link for my replit project

boilerplate-project-exercisetracker - Replit

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