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!

Jeremy where could we find the specs of output like what you have shown above?

In my case I have the data but I don’t know exactly what kind of format they are asking and how it looks like.

And why do they removed the numbers (id?) after the underscore _ for the username? For example the fcc_test_16418661806 becomes fcc_test.

Thank you for your response ahead. GOD bless.

The example output specs are on the project page where you submit your project URL for testing. You can also find them in the curriculum repo by looking at the source for the tests themselves if you really want to. The username is just an example; the ones used have the number attached (it’s the time in milliseconds), but that shouldn’t matter.

If you’re having a problem with the project, it’s probably best to start a new thread if an existing one doesn’t resolve your problem. Many people ignore old or solved threads.

Thank you for your response Jeremy. I appreciate it much.

Yes, I will check on their repo if that’s the case.

And sorry, for virtually hijacking this thread. I actually posted one before this on certification, but I got no response so far. And I just saw an opportunity to have an answer, seeing your response is related to my own question. But anyway, thank you very much for your response. GOD bless.

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