Exercise Tracker: Can't pass 4th and 6th tests

I am working on the Exercise tracker in the API’s and Microservices certification porjects. I am unable to get it to pass the 4th and 6th tests.

4th test: I can add an exercise to any user by posting form data userId(_id), description, duration, and optionally date to /api/exercise/add. If no date supplied it will use current date. App will return the user object with the exercise fields added.

6th test: I can retrieve part of the log of any user by also passing along optional parameters of from & to or limit. (Date format yyyy-mm-dd, limit = int)

As far as I can tell the appo does everything it is supposed to do it just does not pass the tests. I think it may have something to do with how the tests are being returned.

Code for the 4th test POST:

//post exercises
app.post('/api/exercise/add',(req,res)=>{
  console.log('4');
  userModel.findOne({userId:req.body.userid},(err,user) => {
    if(err){
      console.log(err);
      return res.send("user does not exist");
    }
  })
  console.log(req.body.date);
  if(req.body.date === undefined){
    var date = new Date();
  }else{
    var date = new Date(req.body.date);
  }
  
  const exerciseObj = new exerciseModel({
    exerciseDescription:req.body.description,
    duration:parseInt(req.body.duration),
    date:date
  })

userModel.findOne({_id:req.body.userId},(err,foundModel) =>
{
  console.log('3');
  console.log(req.body);
  if(err) return console.log(err);
  foundModel.exercise.push(exerciseObj);
  foundModel.save((err,updatedModel) => {
    if(err) return console.log(err);
  
    return res.json({
      userName: updatedModel.userId,
      description:exerciseObj.description,
      duration:exerciseObj.duration,
      _id:mongoose.Types.ObjectId(exerciseObj._id),
      date:exerciseObj.date.toUTCString()
    });
  })
})
})

Code for the 6th test GET:

//get exercise log
app.get("/api/exercise/log",(req,res)=>{
  console.log('parameters: '+req.query.from);
  userModel.findOne({_id:req.query.userId},(err,foundUser)=>{
    if(err) return console.log(err);
    var startDate = new Date(req.query.from);
    var endDate = new Date(req.query.to);
    var limit = parseInt(req.query.limit);
    var log = [];

    if(req.query.from === undefined && req.query.to === undefined){
      return res.json({userId:foundUser.userId,exerciseCount:foundUser.exercise.length,log:foundUser.exercise});

    }else if(req.query.from != undefined && req.query.to != undefined){
      console.log('startdate: ' + startDate + 'enddate: ' + endDate + 'limit: ' + limit);
      for(var i = 0;i<foundUser.exercise.length;i++){ 
        if(foundUser.exercise[i].date > startDate && foundUser.exercise[i].date < endDate){
          log.push(foundUser.exercise[i]);  
      }}
      if(limit != NaN){
        log = log.slice(0,limit);
      }
      console.log('1')
      console.log('fullexerciselog: ' + foundUser.exercise)
      console.log('UserId: '+ foundUser.userId+ ' _id: '+ foundUser._id+ ' exerciseCount: ' + foundUser.exercise.length + ' log: '+   foundUser.exercise)
      return res.json({userId:foundUser.userId,_id:foundUser._id,exerciseCount:log.length,log:log})
    }else if (req.query.to ===undefined){
      console.log('2')
      for(var i = 0;i<foundUser.exercise.length;i++){
        if(foundUser.exercise[i] > startDate){
          log.push(foundUser.exercise[i]);
        }
      }
      if(limit != NaN){
        log = log.slice(0,limit);
      }
      return res.json({userId:foundUser.userId,exerciseCount:foundUser.exercise.length,log:log})
    }
    })  
    })

Schema:

const Schema = mongoose.Schema;
const exerciseSchema = new Schema({
  exerciseDescription:{type:String, required:true},
  duration:{type:Number, required:true},
  date:{type:Date}
})
const userSchema = new Schema({
  userId:{type:String, required: true,unique:true},
  exercise:[exerciseSchema]
})

Thank you in advance for any help.

I changed the date in the 4th Test POST to:

return res.json({
      userName: updatedModel.userId,
      description:exerciseObj.description,
      duration:exerciseObj.duration,
      _id:mongoose.Types.ObjectId(exerciseObj._id),
      date:exerciseObj.date.toDateString()
    });

It still does not pass the tests.

same, i can’t get pass the 4th test

any update on this? i also cant pass the 4th test but i think i did everything correctly. works as supposed to. thanks for any update!

any update on this regarding the 4th test? cant seem to pass it too but everything works fine on my app. appreciate any info

i passed the test, i only forgot to parseInt my duration.

how about you?

Can’t pass the test it’s freaking me out lol. Thanks for the reply tho. Do you happen to have a copy of your solution? Fingers crossed*** lmao

My Schema

const logSchema = new mongoose.Schema({
  description: {type: String, required: true},
  duration: {type: Number, required: true},
  date: {type: Date, default: () => new Date()} // useless :v
})

const userSchema = new mongoose.Schema({
  username: String,
  log: {type: [logSchema]}
})

My POST

app.route('/api/exercise/add')
    .post((req,res)=>{
      User.findById(req.body.userId,(err,user)=>{
        if(err) return res.send(req.body.userId+" is not a userId")

        // Is the date a number? or a normal string?
        const regex = /[^0-9]/
        if(req.body.date){
          if(regex.test(req.body.date) === true){
            var datt = new Date(req.body.date)
          }
          else{
            var datt = new Date(parseInt(req.body.date))
          }
        }
        else{
          var datt = new Date()
        }
        // datt is the date
        
        user.log.push({
          duration: req.body.duration,
          description: req.body.description,
          date: datt 
        })
        user.save((err,data)=>{
          if(err) return console.log(err)
          res.send({
            _id: req.body.userId, // data.toObject()._id should work as well
            username: data.toObject().username,
            date: datt.toString().slice(0,15),  // response: the correct format
            duration: parseInt(req.body.duration), 
            description: req.body.description
          })
        })
      })
    })

Thank you! Much appreciated!