Back End Development and APIs Projects - Exercise Tracker

Tell us what’s happening:
Describe your issue in detail here.

Your project link(s)
The response returned from

POST /api/users/:_id/exercises

const express = require('express')
const app = express()
const cors = require('cors')
require('dotenv').config()
const mongoose= require('mongoose');
const {Schema} = mongoose;



mongoose.connect(process.env['DB_URL'])

const UserSchema= new Schema({
  username:String
})
const User=mongoose.model("User",UserSchema)

const ExerciseSchema=new Schema({
  user_id: {type: String, required: true},
  description: String,
  duration: Number,
  date: Date
  
});

const Exercise= mongoose.model("Exercise",ExerciseSchema);

app.use(cors())
app.use(express.static('public'))
app.use(express.urlencoded({extended:true}))
app.get('/', (req, res) => {
  res.sendFile(__dirname + '/views/index.html')
});
app.get("/api/users", async (req,res)=>{
  const users = await User.find({}).select("_id username")
  if(!users){
    res.send("no users")
  }else{
    res.json(users);
  }
})



app.post("/api/users",async(req,res)=>{
 
  console.log(req.body)

  const userObj= new User({
    username:req.body.username
  })

  try{
    const user= await userObj.save()
    console.log(user);
    res.json(user)
  }catch(err){
    console.log(err)
  }
})
app.post("/api/users/:_id/exercises", async (req, res) => {
  const id = req.params._id;
  const { description, duration, date } = req.body;
  try {
    const user = await User.findById(id);
    if (!user) {
      res.send("Couldn't find user");
    } else {
      const exerciseObj = new Exercise({
        user_id: user._id,
        description,
        duration,
        date: date ? new Date(date) : new Date(),
      });
      const exercise = await exerciseObj.save();
      res.json({
        id: user._id,
        username: user.username,
        description: exercise.description,
        duration: exercise.duration,
        date: new Date(exercise.date).toDateString(),
      });
    }
  } catch (err) {
    console.log(err);
    res.json("Error Occurred");
  }
});

app.get("/api/users/:_id/logs",async (req,res)=>{
  const {from,to,limit}=req.query;
  const id=req.params._id;
  const user= await User.findById(id);
  if(!user){
    res.send("user not found")
    return
  }
  let dateObj={};
  if(from){
    dateObj["$gte"] =new Date(from)
  }
  if(to){
    dateObj["$lte"] =new Date(to)
  }
  let filter ={
    user_id:id
  }
  if(from||to){
    filter.date=dateObj;
  }
  const exercises= await Exercise.find(filter).limit(+limit??500)

  const log=exercises.map(e=>({
    description: e.description,
    duration: e.duration,
   date: e.date.toDateString()
  }))

  res.json({
    username:user.username,
    count: exercises.length,
    id:user._id,
    log
  })
    
  
})



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

will be the user object with the exercise fields added.

solution: https://boilerplate-project-exercisetracker--pavan-singhsin1.repl.co

Your browser information:

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

Challenge: Back End Development and APIs Projects - Exercise Tracker

Link to the challenge:

Your Replit wasn’t loading for me, but a fork worked.

Look very carefully at your Exercise response object and compare it to the example. One of the properties (the key) is not the same as the example.