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.