Exercise Tracker 4th - 7th Text

My code is working but can’t pass freecodecamp 4th- 7th text

This is the code for text 4

app.post('/api/users/:_id/exercises', (req,res) => {
  let id = req.body[':_id']
  let newExercise = new Exercise({
    description: req.body.description,
    duration: parseInt(req.body.duration),
    date: req.body.date
  })
  
  if(newExercise.date === '' || undefined){
     newExercise.date = new Date().toISOString().substring(0,10)
  }
  //609857f24b43a904e7b5166c
   Person.findByIdAndUpdate(id,
         {$push: {log: newExercise}},
          {new: true},
          (err, updatedExer)=> {
           if(!updatedExer){
           res.json("invalid userId")  
         }else{
      
            let resObject = {}
            resObject['_id'] = updatedExer.id
            resObject['username'] = updatedExer.username
            resObject['date'] = newExercise.date.toDateString()
             resObject['duration']= newExercise.duration
             resObject['description'] = newExercise.description
            res.json(resObject) 
         }
          }
   )

})

This is the code for 5th-7th text

app.get('/api/users/:_id/logs', (req,res) => {
  Person.findById(req.params._id, (err, result) => {
    if(!err){
      let resObject = result
      
      
      if(req.params.from || req.params.from){
        
        let fromDate = new Date(0)
        let toDate = new Date()
        
        if(req.params.from){
          fromDate = new Date(req.params.from)
        }
        
        if(req.params.to){
          toDate = new Date(req.params.to)
        }
        
        fromDate == fromDate.getTime()
        toDate = toDate.getTime()
        
        resObject.log = resObject.log.filter((exercise) => {
          let exerDate= new Date(exercise.date).getTime().toDateString()
          
          return exerDate >= fromDate && exerDate <= toDate
        })
      }
      
      if(req.params.limit){
        resObject.log = resObject.log.slice(0, req.params.limit)
      }
      
       resObject = resObject.toJSON()
      resObject['count'] = result.log.length
      res.json(resObject)
    }
  })
 
 
})

Your project link(s)

Project link: Glitch :・゚✧

Your browser information:

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

Challenge: Exercise Tracker

Link to the challenge:

Welcome there,

Some things to note:

  • Looking at the browser console, you can debug why the tests might be failing:
    • CORS error: Likely due to app throwing an uncaught error during tests.
  • The 4th user story is:

You can POST to /api/users/:_id/exercises with form data description , duration , and optionally date .

You have:

app.post('/api/users/:_id/exercises', (req,res) => {
  const id = req.body[':_id']
  • There should be no :_id property from the request body, because the form data only contains what is mentioned in the user story. The id is in the request parameter. Are you managing to get anything from req.body[':_id']?
  • The expected date format is a Date String (hint: use the .toDateString method:
newExercise.date = new Date().toISOString().substring(0,10)
  • Why do you get the id property twice here (just caught my eye):
let id = req.params['_id']
  console.log(id)
  Person.findById(req.params._id,
  • A lot of this date logic is unnecessary, and there is a small typo:
if(req.params.from || req.params.to){
        
        let fromDate = new Date(0)
        let toDate = new Date()
        
        if(req.params.from){
          fromDate = new Date(req.params.from)
        }
        
        if(req.params.to){
          toDate = new Date(req.params.to)
        }
        
        fromDate == fromDate.getTime()
        toDate = toDate.getTime()
        
        resObject.log = resObject.log.filter((exercise) => {
          let exerDate= new Date(exercise.date).getTime().toDateString()
          
          return exerDate >= fromDate && exerDate <= toDate
        })
  • Date objects in JS can be compared in any format - there is no need to convert them to a time.

I hope some of this helps

Thanks for the reply, when i checked the console after submitting the text most of the error is about CORS policy. and i tried to solve it by adding the necessary code but am still having the same problem.
I also notice that the id is null when FCC is testing code

app.post('/api/users/:_id/exercises', (req,res) => {
  const id = req.body[':_id']
  const {description, duration, date} = req.body
   const dateObj = date === '' ? new Date() : new Date(date)
  let newExercise = new Exercise({
    description,
    duration,
    date:new Date(dateObj).toDateString()
  })
  
  //609fb3f4af5e2900d278696e
   Person.findByIdAndUpdate(id,
         {$push: {log: newExercise}},
          {new: true},
          (err, data)=> {
           if(!err){
            let resObject = {}
            resObject['_id'] = data.id
            resObject['username'] = data.username
            resObject['date'] = new Date(dateObj).toDateString()
             resObject['duration']= newExercise.duration
             resObject['description'] = newExercise.description
            res.json(resObject) 
         }else{
            res.json("invalid userId")
         }
          }
   )

})
type or paste code here

I notice you commented out the cors:

//app.use(cors())

Why?

If it is not working, it might be worth changing the version in the package.json

Thanks so much bro, I have passed the test. I was using req.body instead of req.params

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