Back End Development and APIs Projects - Exercise Tracker

Tell us what’s happening:
Describe your issue in detail here.

Test 8 is the only test I failed.
I don’t know why. I think it might be the error of FCC test code itself.
Below is my code. please help.

const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const shortid = require('shortid');
require('dotenv').config();

const app = express();
app.use(cors());
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));

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

const users = [];
const exercises = [];

app.get('/api/users', (req, res) => {
  res.json(users);
});

app.post('/api/users', (req, res) => {
  const user = {
    username: req.body.username,
    _id: shortid.generate({ length: 10 }),
  };
  users.push(user);
  res.json(user);
});

app.post('/api/users/:_id/exercises', (req, res) => {
  const user = users.find((user) => user._id === req.params._id);
  if (user) {
    const exercise = {
      ...user,
      date: req.body.date || new Date().toDateString(),
      duration: Number(req.body.duration),
      description: req.body.description,
    };
    exercises.push(exercise);
    res.json(exercise);
  } else {
    res.status(404).send();
  }
});

app.get('/api/users/:_id/logs', (req, res) => {
  const user = users.find((user) => user._id === req.params._id);
  if (user) {
    let log = [];
    let count = 0;
    exercises.forEach((exercise) => {
      if (exercise._id === req.params._id) {
        log.push({
          description: exercise.description,
          duration: exercise.duration,
          date: exercise.date,
        });
        count += 1;
      }
    });

    if (req.query.from) {
      log.filter((l) => new Date(l.date) >= new Date(req.query.from));
    }
    if (req.query.to) {
      log.filter((l) => new Date(l.date) <= new Date(req.query.to));
    }
    if (req.query.limit) {
      log = log.slice(0, Number(req.query.limit));
    }

    res.json({
      username: user.name,
      count: count,
      _id: req.params._id,
      log: log,
    });
  } else {
    res.status(404).send();
  }
});

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

Your project link(s)

Your browser information:

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

Challenge: Back End Development and APIs Projects - Exercise Tracker

Link to the challenge:

Welcome to the forums!

I’ve edited your post, as your code needs to be enclosed in triple backticks to make it legible.

Could you also provide a link to your code please, on replit or wherever you have it hosted?

Thanks! Actually, I’ve not had my code hosted anywhere. I tested it on my local machine. localhost:3000 worked anyway.

You are not constructing the Date in the correct format using req.body.date

Also, the project has to be live, you can not use the localhost version for the final submission.