Exercise Tracker (backend certification) / Not passing test case for `POST /api/users/:_id/exercises`

Tell us what’s happening:
I’m trying to pass the exercise tracker for the backend certification but struggling with passing the following spec:

The response returned from POST /api/users/:_id/exercises will be the user object with the exercise fields added.

Yet, what I’m returning looks exactly the same as in the instruction, so I don’t get what test case I’m not passing. I’ve spent quite a while on it and am clueless at this stage.

I know it’s quite a lot of code (or at least for me), but if anyone could help or give feedback, that’d be awesome.

The route to post new exercises is the following one:

//Get all users route
router.get("/api/users", async (req, res) => {
    try {
        const users = await User.find();
        res.status(200).json(users);
    }
    catch(error) {
        res.status(400).json({error: error.message});
    }
});

//Create new exercice route
router.post("/api/users/:_id/exercises", isExistingUser, async (req, res) => {
    try {
        const userFound = req.user;
        const date = req.fields.date? new Date(req.fields.date): new Date();
        const exercice = new Exercice({
            description: req.fields.description,
            duration: req.fields.duration,
            date: date.toDateString(),
            parsedDate: Date.parse(date),
            user: userFound._id
        })
        await exercice.save();
        res.status(200).json({
            username: userFound.username,
            description: req.fields.description,
            duration: req.fields.duration,
            date: date.toDateString(),
            _id: userFound._id
        });
    }
    catch(error) {
        res.status(400).json({error: error.message})
    }
});

The model I used for exercises is:

const mongoose = require("mongoose");
const User = require("./User");

const Exercice = mongoose.model("Exercice", {
    description: String,
    duration: Number,
    date: String,
    parsedDate: Number,
    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: User}
});

module.exports = Exercice

I also added a middleware to check if the user was already existing before adding a new exercise:

const User = require("../Models/User");

const isExistingUser = async (req, res, next) => {
    console.log("entering middleware...");
    const userId = req.params._id;
    const userFound = await User.findById(userId);
    if (userFound) {
        req.user = userFound;
        next();
    }
    else {
        res.status(200).json({message: "No user corresponding to this ID"});
    }
}

module.exports = isExistingUser;

Your project link(s)

solution: https://exercice-tracker-jm.herokuapp.com
github: GitHub - JossMilon/fcc-exercice-tracker: 4th exercice to pass the back end certification - https://www.freecodecamp.org/learn/back-end-development-and-apis/back-end-development-and-apis-projects/exercise-tracker

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Exercise Tracker

Link to the challenge:

Log this output before you send it, and it looks like

{
  username: 'fcc_test_16418661806',
  description: 'test',
  duration: '60',
  date: 'Tue Jan 02 1990',
  _id: new ObjectId("61dce3c41617e0272f8275e7")
}

The spec asks for

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

The test doesn’t like your duration.

1 Like

Wow, thanks so much Jeremy, should have paid attention to this detail, especially with the color coding, many thanks again! One more to go!