Exercise tracker - tests 11-15 won't pass

Hi everyone!
I’m struggling passing the 11-15 tests.
I’m guessing it’s because my exercise objects in the log contain the _id key-value pair.
The output:
output

What did I do wrong and how can I fix it?

Thanks in advance :grinning:

My project

My code:

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

//conect to DB 
mongoose.connect(process.env.MONGO_URL)

const exersiceSchema = new Schema({
  date: String,
  duration: Number,
  description: String
})

const userSchema = new Schema({
  username: {
    type: String,
    required: true
  },
  log: [exersiceSchema]
})

const User = mongoose.model('User', userSchema)
const Exercise = mongoose.model('Exercise', exersiceSchema)

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


app.route('/api/users')
  // 2-3: creat a user when submmitimg a username
  .post((req, res) => {
    const username = req.body.username
    const user = new User({ username })
    user.save((err, data) => {
      if (err || !data){
        res.send("There's an error saving the user");
      }
      res.json(data);
    })
  })
  // 4-6: get list of users
  .get((req, res) => {
    User.find((err, users) => {
      if(!err){
        res.json(users);
      }
  })
})

// 7-8: add exercies to a user 
app.post('/api/users/:_id/exercises', (req, res) => {
  const id = req.params._id;
  const { description } = req.body;
  const duration = parseInt(req.body.duration);
  const date = req.body.date ? new Date(req.body.date) : new Date();
  const newExercise = new Exercise({
        date: date.toDateString(),
        duration: duration,
        description: description
      })
  User.findByIdAndUpdate(id, {$push: {log: newExercise}}, {new: true}, (err, user) => {
    if(!user || err){
      res.send("Cannot find user");
    }
    else {
      newExercise.save((err, data) => {
        if(err){
          res.send("There's an error saving the exercise");
        }
        else {
        res.json({
          _id: id,
          username: user.username,
          date: newExercise['date'],
          duration: newExercise['duration'],
          description: newExercise['description']
        });
        }
      })
    }
  }) 
})

// 9-10: retrieve a full exercise log of any user
app.get('/api/users/:_id/logs', (req, res) => {
  //const { from, to } = req.query;
  const id = req.params._id;
  //const { limit } = req.query.limit ? req.query.limit : 10;
  User.findById(id, (err, user) => {
    if(err || !user){
      res.send("There's an error retrieving the exercise log");
    } else {
      let count = user.log.length;
      res.json({
        user,
        count
               });
      
      
    }
  })
})

// checking if the connection is good
app.get('/mongo-health', (req, res) => {
  res.json({status: mongoose.connection.readyState})
})

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

It’s not the extra fields. Here’s the route input and output for the GET logs route:

req.body: {}
req.params: {"_id":"623a6f605ac3932e6b21ecc7"}
req.query: {}
{
  "user": {
    "_id": "623a6f605ac3932e6b21ecc7",
    "username": "fcc_test_16479967687",
    "log": [
      {
        "date": "Wed Mar 23 2022",
        "duration": 60,
        "description": "test",
        "_id": "623a6f605ac3932e6b21ecc9"
      }
    ],
    "__v": 0
  },
  "count": 1
}

Here’s the spec:

{
  username: "fcc_test",
  count: 1,
  _id: "5fb5853f734231456ccb3b05",
  log: [{
    description: "test",
    duration: 60,
    date: "Mon Jan 01 1990",
  }]
}

You’ve got the correct fields in incorrect places.

Thank you so much! It worked! :grinning: :grinning:

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