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

I cannot Exercise Tracker task#8 which : The response returned from POST /api/users/:_id/exercises will be the user object with the exercise fields added.
I tried a lot and all tasks I passed except this one.
my code is:


const express = require('express')
const app = express()
const cors = require('cors')
require('dotenv').config()

// mycode 

let mongoose = require('mongoose')
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => console.log('MongoDB connected'))
  .catch(err => console.log(err));




// until here

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




// my code
const { Schema } = mongoose;


const userSchema = new Schema({
  username: { type: String, required: true, unique: true },
  count: { type: Number, default: 0 },
  log: [{
    description: { type: String, required: true },
    duration: { type: Number, required: true },
    date: { type: Date, required: false }
  }]


});

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



// username


app.post('/api/users', async (req, res) => {
  const { username } = req.body;
  if (!username) {
    return res.status(400).json({ error: 'Username is required' })
  }


  try {
    const user = new User({ username });
    const savedUser = await user.save();
    res.json({
      username: savedUser.username,
      _id: savedUser._id
    });
  } catch (err) {
    console.log(err);
    res.status(500).json({ error: 'Server error' })
  }

})


app.get('/api/users', async (req, res) => {

  try {
    const users = await User.find({});
    res.json(users.map((user) => ({
      username: user.username,
      _id: user._id
    })))
  } catch (err) {
    console.log(err);
    res.status(500).json({ error: 'Server error' });
  }




})

// add exercises


app.post('/api/users/:_id/exercises', async (req, res) => {


  try {
    const { _id } = req.params;
    let { description, duration, date } = req.body;


    if (!_id) {
      return res.status(400).json({ error: 'User ID is required' });
    }
    if (!description) {
      return res.status(400).json({ error: 'Description is required' });
    }
    if (isNaN(duration)) {
      throw new Error('Duration must be a number');
    }

    let user = await User.findById(_id);

    if (!user) {
      return res.status(400).json({ error: 'Invalid user ID' });
    }


    const exercise = {
      description,
      duration: parseInt(duration),
      date: date ? (new Date(date)).toDateString() : (new Date()).toDateString(),

    };



    user.log.push(exercise);
    user.count = user.log.length;
    user = await user.save();





    res.json({

      username: user.username,
      description: exercise.description,
      duration: exercise.duration,
      _id: _id,
      date: new Date(exercise.date).toDateString(),



    });



  } catch (err) {
    console.log(err);

    if (err.name === 'ValidationError') {
      res.status(400).json({ error: err.message });
    } else {
      res.status(500).json({ error: 'Internal server error' });
    }
  }





})







// logs



app.get('/api/users/:_id/logs', async (req, res) => {
  const { from, to, limit } = req.query;
  const userId = req.params._id;

  try {

    const user = await User.findById(userId);

    if (!user) {
      return res.status(400).json({ error: 'Invalid user ID' });
    }

    let log = user.log;



    if (from) {
      log = log.filter((exercise) => new Date(exercise.date) > new Date(from));
    }

    if (to) {
      log = log.filter((exercise) => new Date(exercise.date) < new Date(to));
    }

    if (limit) {
      log = log.slice(0, limit);
    }

    res.json({
      _id: user._id,
      username: user.username,
      count: user.count,
      log: log.map((exercise) => {

        return {
          description: exercise.description,
          duration: exercise.duration,
          date: (new Date(exercise.date)).toDateString()
        }
      }),
    });


  }



  catch (err) {
    console.log(err);
    res.status(500).json({ error: 'Server error' });
  }


})



// until here



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

You are more likely to get help if you post a Replit or a repo.

Your code is passing for me, did you fix it?

No I’m handling it locally on VS code. I couldn’t fix it yet.

It has to be online anyway for the final submission so try it on Replit.

When I use your code with my own DB on MongoDB Atlas it passes all the tests (using Replit).

Thank you! I think This is the reason.

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