Exercise Tracker - does not pass the tests

Tell us what’s happening:
The code does not pass the fourth test. Why???

Your code so far

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

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


app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());


mongoose.connect(process.env.DB_URI, { useNewUrlParser: true, useUnifiedTopology: true });
mongoose.connection.on('connected', () => {
  console.log('connected');
  console.log(mongoose.connection.readyState);
});

let clientSchema = new mongoose.Schema({
  username: {type: String, required: true}, 
  _id: { type: String, required: true }
});
let Client = mongoose.model('Client', clientSchema);

let exerciseSchema = new mongoose.Schema({
  userId: String,
  description: {type: String, required: true},
  duration: {type: Number, required: true},
  date: Date
});
let Session = mongoose.model('Session', exerciseSchema);


app.post('/api/exercise/new-user', (req, res) => {
  let user = req.body.username;
  let generateID = mongoose.Types.ObjectId();

  Client.create({
    username: user,
    _id: generateID
  });

  res.json({
    username: user,
    _id: generateID
  });
});

app.get('/api/exercise/users', (req, res) => {
  Client.find({}, (err, result) => {
    if(!err){
      res.json(result);
    }
  });
});



app.post('/api/exercise/add', (req, res) => {
  let { userId, description, duration, date } = req.body;
  let dateAuto;

  if(date === ''){
          dateAuto = new Date().toISOString().substring(0, 10);
        }


  Client.findById(userId, (err, data) => {
      if(!data){
      res.send('Unknown userId');
      } else {
          Session.create({
          userId: userId,
          description: description,
          duration: parseInt(duration),
          date: date
       });
      }
  });

  Client.findByIdAndUpdate(
    userId, { $push: { log: Session } }, { new: true }, (err, update) => {
      if(!err){
        let responseObject = {};
        responseObject['_id'] = userId;
        responseObject['username'] = update.username;
        responseObject['date'] = new Date(dateAuto).toDateString();
        responseObject['duration'] = duration;
        responseObject['description'] = description;

        res.json(responseObject);
      }
    }
  )
});



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

Your browser information:

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

Challenge: Exercise Tracker

Link to the challenge:

Hey there,

please do not create multiple topics about the same issue.

I already gave you an answer here:


Hey there,

please don’t misuse additional posts or the flag-feature to get answers.

You can have a look at the tests on github .

Go to the failing test and see if you can understand the test code.

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