Why don't pass the test?

Hello. Context: API ( Exercise tracker)

My POST endpoint:

app.post('/api/users/:_id/exercises', function(req,res){
  // obtiene los datos del request
  const id = req.params._id;
  const exercise = {
    description: req.body.description,
    duration: Number(req.body.duration),
    date: new Date(req.body.date).toDateString()
  };
  // busca el obj en la db y lo trae
  User.findById(id, function(error,usuario){
    if(error){
      console.log(error)
    }
    // agrega el ejercicio a la lista
    usuario.log.push(exercise);
    usuario.count ++;
    usuario.save()
      .then((data)=>{
        res.json({
          _id: data._id,
          username: data.username,
          date: exercise.date,
          duration: exercise.duration,
          description: exercise.description
        })
      })
      .catch((error)=>{
        res.json({error: error})
      })
  })
});

Please post a link to a live app on something like Replit we can actually test (or at least a repo with the code).

Your data doesn’t seem correct, the description should be “test” and the date should be “Mon Jan 01 1990”. You are also not handling the date as optional in the code you posted. Not sure if you have something in the Schema for the date or what.

You can see the expected values if you look at the tests as well.

https://github.com/freeCodeCamp/freeCodeCamp/blob/main/curriculum/challenges/english/05-back-end-development-and-apis/back-end-development-and-apis-projects/exercise-tracker.md

Hello. Terminate the api and it works the same as the example.
2 tests do not pass and I do not know why (the results are as expected).
Full code:
PLS Help!

const express = require('express')
const app = express()
const cors = require('cors')
require('dotenv').config()
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bodyParser = require('body-parser');
mongoose.connect('mongodb+srv://pantera51:pantera515151@cluster0.cn1exm6.mongodb.net/test')

const user = new Schema({
  username: {
    type: String,
    required: true
  },
  count: Number,
  log: [Object]
})
let User = mongoose.model("User", user)

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

app.post('/api/users', function(req,res){{
  const user = new User({username: req.body.username, count: 0})
  user.save()
      .then((ysy)=>{
        res.json({
          username: ysy.username,
          _id: ysy._id
        })
      })
}});

app.post('/api/users/:_id/exercises', function(req,res){
  // obtiene los datos del request
  const id = req.params._id;
  const date = !req.body.date? new Date().toDateString():new Date(req.body.date).toDateString()
  const exercise = {
    description: req.body.description,
    duration: Number(req.body.duration),
    date: date
  };
  // busca el obj en la db y lo trae
  User.findById(id, function(error,usuario){
    if(error){
      console.log(error)
    }
    // agrega el ejercicio a la lista
    usuario.log.push(exercise);
    usuario.save()
      .then((data)=>{
        res.json({
          _id: data._id,
          username: data.username,
          date: exercise.date,
          duration: exercise.duration,
          description: exercise.description
        })
      })
      .catch((error)=>{
        res.json({error: error})
      })
  })
});

app.get('/api/users', function(req,res){
  (async()=>{
    const resultado = await User.find({});
    let rta = [];
    for(let x of resultado){
      let obj = {
        _id: x._id,
        username: x.username
      }
      rta.push(obj)
    }
    res.send(rta);
  })();
})

app.get('/api/users/:_id/logs',function(req,res){
  const id = req.params._id;
  const from = Date.parse(req.query.from);
  const to = Date.parse(req.query.to);
  const limit = Number(req.query.limit);
  let rta = {};
  User.findById(id,function(error,data){
    if(error){
      console.log(error);
    } else {
      rta._id = data._id;
      rta.username = data.username;
      if(from){
        rta.from = new Date(req.query.from).toDateString();
        rta.to = new Date(req.query.to).toDateString();
        rta.count = 0;
        rta.log = data.log.filter(item => Date.parse(item.date) > from && Date.parse(item.date) < to);
        if(limit){
        rta.log = data.log.slice(0,limit)
        }
      } else {
        rta.log = data.log;
      }  
      rta.count = rta.log.length;
      res.json(rta);
    }
})
})


const listener = app.listen(process.env.PORT || 3000, () => {
  console.log('Your app is listening on port ' + listener.address().port)
})
  1. Your limit code is inside a condition that is never met. Look at the limit request URL does it contain from?

  2. You are adding properties to the response object you were not asked to add. In this case, the test doesn’t break because of it, but it is a bad idea to change the expected response as the tests might do an object compare to an expected object.


You should probably remove the connection string and change your password so people can’t use your DB. It isn’t a big deal when asking for help but you definitely do not want to leak your DB connection string like that without changing it later.

I was able to solve it by observing better the help that they had given me… thanks