Exercise Tracker Lesson (Without DB)

Hi everyone,

So i’ve tried to solve this (https://www.freecodecamp.org/learn/back-end-development-and-apis/back-end-development-and-apis-projects/exercise-tracker) exercise without a DB (because i’m learning PostgreSQL and if i was to use i’d use Postgres) i don’t see any problem since the text of the lesson didn’t said it was necessary, but despite thinking my code as correct and watching the test output i didn’t found out what is exactly wrong in my code ( i don’t think there’s anything wrong honestly since the output that i made is exactly what the tests ask to), my tests get wrong from the one below till the end.

The response returned from POST /api/users/:_id/exercises will be the user object with the exercise fields added.
To the end, all above this one gets correct output.

And i’ll post here my code:

const express = require('express');
const app = express();
const cors = require('cors');
require('dotenv').config();

app.use(cors());
app.use(express.static('public'));
app.use(express.json());
app.use(express.urlencoded({ extended: true}));

let list_of_users = [];

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

app.get('/api/users/', (req,res) => {
  res.send(list_of_users);
});
  
app.post('/api/users/', (req,res) => {
  let new_user = {};
  let myUsername = req.body.username;
  let myId = Date.now().toString(36) + Math.random().toString(36).substr(2);

  new_user['username'] = myUsername;
  new_user['_id'] = myId;
  list_of_users.push(new_user);
  
  res.json({
    username: myUsername,
    _id: myId
  });
});

app.post('/api/users/:_id/exercises', (req,res) => {
  
  console.log(req.params._id);
  // let indexToInsert = list_of_users.indexOf(req.params._id);
  let indexToInsert = list_of_users.findIndex(x => x._id === req.params._id);
  console.log(indexToInsert);
  list_of_users[indexToInsert]['description'] = req.body.description;
  list_of_users[indexToInsert]['duration'] = req.body.duration;
  list_of_users[indexToInsert]['date'] = new Date(req.body.date).toDateString();

  // console.log(typeof(list_of_users[indexToInsert]));

  // list_of_users[indexToInsert] = req.body;
  if(new Date(req.body.date).toDateString() === 'Invalid Date')
    list_of_users[indexToInsert]['date'] = new Date().toDateString();
  
  res.json({
    username: list_of_users[indexToInsert]['username'],
    description:list_of_users[indexToInsert]['description'], 
    duration: list_of_users[indexToInsert]['duration'],
    date: list_of_users[indexToInsert]['date'],
    _id: list_of_users[indexToInsert]['_id']
  });
});

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

It would have been nice with a link to a live version please so we don’t have to set it all up just to test it.

If you look at your response and the expected or just the example that is given can you spot an issue? Are all the types as expected?

Example:

{
  username: "fcc_test"
  description: "test",
  duration: 60,
  date: "Mon Jan 01 1990",
  _id: "5fb5853f734231456ccb3b05"
}

It would have been nice with a link to a live version please so we don’t have to set it all up just to test it.

Got ya champ! https://fcc-exercise-tracker1234.herokuapp.com/

Are all the types as expected?

Yes, even the date

Are you sure about that? Did you actually check it?

Your response, compare it to the example. Look at the value types.

{
    "username": "fcc_test_16370967682",
    "description": "test",
    "duration": "60",
    "date": "Tue Nov 16 2021",
    "_id": "kw2l5abiuo1baq17nzp"
}
1 Like

Hey, thank you very much the test were i was failing now is approved, i get you i was not converting duration to Number type! thanks!!!