Back End Development and APIs Projects - Exercise Tracker

Tell us what’s happening:
Can’t seem to pass the POST exercises test or the from, to, limit from logs test. I’ve console.logged both parts of the code and they appear to be returning what is required in the FCC test validator says they need to return, so not sure what I’m missing here.

Your project link(s)

solution: boilerplate-project-exercisetracker - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0

Challenge: Back End Development and APIs Projects - Exercise Tracker

Link to the challenge:

Hello , I have the same issue.

I can’t can’t pass the following test :


The response returned from POST /api/users/:_id/exercises will be the user object with the exercise fields added.

And this is what I’am returning :


{
"_id":"6419baaf6758495065a40a74",
"username":"tomas",
"__v":0,
"description":"pernas",
"duration":15,
"date":"Tue Mar 21 2023"
}

To give more context, for the POST exercises condition, this is what is being returned:

{
username: ‘fcc_test_16794124610’,
description: ‘test’,
duration: ‘60’,
date: ‘Mon Jan 01 1990’,
_id: ‘6419cced72cc5a40f5f2f192’
}

and for the “from, to, limit” logs test, I’m returning this:

{
username: ‘fcc_test_16794102128’,
_id: ‘6419c4242c19bcdb8a59b539’,
count: 2,
log: [
{
description: ‘test’,
duration: 60,
date: ‘Mon Jan 01 1990’,
_id: new ObjectId(“6419c4252c19bcdb8a59b53c”)
},
{
description: ‘test’,
duration: 60,
date: ‘Wed Jan 03 1990’,
_id: new ObjectId(“6419c4252c19bcdb8a59b540”)
}
]
}

  1. Look at the data type of duration in your response for the POST to /api/users/:_id/exercises and compare it to the example.
{"username":"fcc_test_16794139087","description":"test","duration":"60","date":"Mon Jan 01 1990","_id":"6419d29572cc5a40f5f2f8b3"}
  1. Look at the last response to logs?limit=1

Request URL: https://boilerplate-project-exercisetracker.brand421.repl.co/api/users/6419d29b72cc5a40f5f2f8ee/logs?limit=1

{"username":"fcc_test_16794139154","_id":"6419d29b72cc5a40f5f2f8ee","count":2,"log":[{"description":"test","duration":60,"date":"Mon Jan 01 1990","_id":"6419d29c72cc5a40f5f2f8f1"},{"description":"test","duration":60,"date":"Wed Jan 03 1990","_id":"6419d29c72cc5a40f5f2f8f5"}]}

@izzypt Please open up your own thread to get help and post your Replit link.

1 Like

Okay I was able to fix the duration being a string instead of an integer, but I’m still struggling with the log issue. I’ve tried a number of things, including adding a conditional findByID based on the limit number, but I don’t understand why the log isn’t being limited.

The to/from and limit are two separate tests.

/logs?from=1989-12-31&to=1990-01-04
/logs?limit=1

Your condition

if (fromDate && toDate && limit)

Are they all truthy with the two different URLs?

You are setting limit to 0 when it is undefined and that will coerce to false.

let limit = req.query.limit || 0;
if (true && true && 0) {
  console.log('true');
} else {
  console.log('false'); // false
}

Okay I’ve been trying to tweak my code the past couple of days (even rewriting it a couple of times to make sure the logic made sense), and I’m still not sure why it’s not limiting the log results. There’s an issue with the limit function that I’m doing wrong in the query but I’m not sure what it is.

Alright, here’s the breakdown and walkthrough of my code:

First I’m checking if there’s a from and to date range, then if there is, checking to see if there’s a limit

if (fromDate && toDate) {
fromDate = new Date(fromDate);
toDate = new Date(toDate);
if (fromDate == ‘Invalid Date’ || toDate == ‘Invalid Date’) {
return res.json(‘Invalid parameters’);
}
if (limit) {
if (limit <= 0) {
return res.json(‘Invalid number’);
} else {
user.findById({_id: id, date: {$gte: fromDate, $lte: toDate}})
.limit(limit)
.then(function(logs, err){
if(err) console.error(err);
let log = logs.log;
let count = limit;
console.log({username: logs.username, _id: id, limit: limit, count: limit, log: log});
res.json({username: logs.username, _id: id, count: limit, log: log});
})
}
user.findById({_id: id, date: {$gte: fromDate, $lte: toDate}})
.then(function(logs, err){
if(err) console.error(err);
let log = logs.log;
let count = log.length;
console.log({username: logs.username, _id: id, count: count, log: log});
res.json({username: logs.username, _id: id, count: count, log: log});
})
}
}

If there is no date, we then look for just a limit. This seems like where it’s going wrong, as when I try to return a limited number of results, the program doesn’t limit the results, even when using ‘.limit’ in the ‘findById’ function

else {
if (limit) {
if (limit <= 0) {
return res.json(‘Invalid number’);
} else {
user.findById({_id: req.params._id})
.limit(limit)
.then(function(logs, err) {
if(err) console.error(err);
let log = logs.log;
let count = limit;
// console.log({username: logs.username, _id: id, limit: limit, count: limit, log: log});
res.json({username: logs.username, _id: id, count: limit, log: log});
})
}
}
user.findById({_id: req.params._id})
.then(function(logs, err){
if(err) console.error(err);
let log = logs.log;
let count = log.length;
// console.log({username: logs.username, _id: id, count: count, log: log});
res.json({username: logs.username, _id: id, count: count, log: log});
})
}

As I said, they are two different tests.

fromDate and toDate will not be truthy when running the limit test. limit=1 is the only query param you are given for that test so having that condition inside the outer condition doesn’t work.

if (false && false) {
  if (true) {
    console.log('never runs');
  }
}

Also, I’m not sure .limit(limit) on user.findById is doing what you want it to.

I understand that, I have the GET request split up by whether or not there are dates entered. There’s one part of the code looking for the from and to date params:

if (fromDate && toDate) {
fromDate = new Date(fromDate);
toDate = new Date(toDate);
if (fromDate == ‘Invalid Date’ || toDate == ‘Invalid Date’) {
return res.json(‘Invalid parameters’);
}
if (limit) {
if (limit <= 0) {
return res.json(‘Invalid number’);
} else {
user.findById({_id: id, date: {$gte: fromDate, $lte: toDate}})
.limit(limit)
.then(function(logs, err){
if(err) console.error(err);
let log = logs.log;
console.log({username: logs.username, _id: id, limit: limit, count: limit, log: log});
res.json({username: logs.username, _id: id, count: limit, log: log});
})
}
user.findById({_id: id, date: {$gte: fromDate, $lte: toDate}})
.then(function(logs, err){
if(err) console.error(err);
let log = logs.log;
let count = log.length;
console.log({username: logs.username, _id: id, count: count, log: log});
res.json({username: logs.username, _id: id, count: count, log: log});
})
}
}

Then there’s an else statement that is for if there is a limit param or not

else {
if (limit) {
if (limit <= 0) {
return res.json(‘Invalid number’);
} else {
user.findById({_id: req.params._id})
.limit(limit)
.then(function(logs, err) {
if(err) console.error(err);
let log = logs.log;
// console.log({username: logs.username, _id: id, limit: limit, count: limit, log: log});
res.json({username: logs.username, _id: id, count: limit, log: log});
})
}
}
user.findById({_id: req.params._id})
.then(function(logs, err){
if(err) console.error(err);
let log = logs.log;
let count = log.length;
// console.log({username: logs.username, _id: id, count: count, log: log});
res.json({username: logs.username, _id: id, count: count, log: log});
})
}

Also, by putting .limit(limit), i thought the findById json would use the limit number entered based on the req.query.limit to determine how many users would return. Am I using it wrong?

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