Exercise tracker test broken?

I’ve tested all the requirements manually, and apparently, they are OK, but it doesn’t pass the route GET /api/users/:_id/logs test. The test before the last one. Here is my code, please kindly help me out.

const express = require('express')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const app = express()
const cors = require('cors')
require('dotenv').config()

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

const exerciseSchema = new mongoose.Schema({
  description: String,
  duration: Number,
  date: String,
})

const userSchema = new mongoose.Schema({
  username: {
    type: String,
    unique: true
  },
  log: [exerciseSchema]
}, {versionKey: false});

const User = mongoose.model('User', userSchema);

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

app.post('/api/users', function(req, res) {
  let username = req.body.username;
  User.findOne({username}, (err, foundUser) => {
    if (foundUser) {
      let {_id} = newUser;
      return res.json({
        username,
        _id
      });
    }
    User.create({username}, (err, newUser) => {
      let {_id} = newUser;
      return res.json({
        username,
        _id
      });
    })
  })
});

app.get('/api/users', function(req, res) {
  User.find({}, (err, users) => {
    const filteredUsers = users.map((value) => {
      let {username, _id} = value
      return {
        username,
        _id
      };
    });
    res.json(filteredUsers);
  })
});

app.post('/api/users/:_id/exercises', (req, res) => {
  User.findById(req.params._id, (err, foundUser) => {
    let date = req.body.date;
    let {description, duration} = req.body;
    if (date == '') {
      date = new Date().toDateString();
    } else {
      date = new Date(date).toDateString();
    }
    foundUser.log.push({description, duration, date});
    foundUser.save((err, user) => {
      let {username, _id} = user;
      res.json({
        username,
        description,
        duration: parseInt(duration),
        date,
        _id
      });
    });
  })
});

app.get('/api/users/:_id/logs', (req, res) => {
  User.findById(req.params._id, (err, foundUser) => {
    let log = foundUser.log.map((value) => {
      let {description, duration, date} = value;
      return {
        description,
        duration,
        date
      };
    });
    let {username, _id} = foundUser;
    res.json({
      username,
      count: foundUser.log.length,
      _id,
      log
    });
  })
});

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

No, the tests are functioning.

Log the route name, the route inputs, and every response for each route to track what is actually happening in your routes. I would check specifically the date handling in the POST exercise route since many people fail tests because they don’t handle missing dates correctly and problems there cause problems with other tests.

If you continue to have trouble finding the problem, post a link to a repl (preferably replit.com) so that we can debug it.

Thanks Jeremy, I finally fixed it. It was due to the date handling as I didn’t put into consideration invalid dates.

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