Exercise tracker tests failing but same output as sample project?

Hi, I`m working on the exercise tracker project in the backend development module, and my project seems to do everything the sample project does, but still fails to pass most of the tests. Any ideas?

Here is my code:

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

app.use(cors())
app.use(express.static('public'))

const mongoose = require('mongoose');
// connecting to database
const mySecret = process.env['EXERCISE_TRACKER']
mongoose.connect(mySecret, { useNewUrlParser: true, useUnifiedTopology: true }).catch((err) => console.log(err));

// body-parser---------------
app.use(express.urlencoded({ extended: false }))

// mongoose model/schema
//  create mongoose schema
const workoutSchema = new mongoose.Schema({
  description: String,
  duration: Number,
  date: String
})

const userSchema = new mongoose.Schema({
  username: {type: String},
  log: Array
});

let userData = mongoose.model("Dataset", userSchema);
// html page -----------
app.get('/', (req, res) => {
  res.sendFile(__dirname + '/views/index.html')
});

// post for creating users from username -------
app.post('/api/users', (req, res) => {

  // console.log(req.body)
  // var declarations ---------------
  let user = req.body.username;
  let userProfile = new userData({
    username: user,
    
  })

  // save new profile -------------- 
  userProfile.save()
    .catch(err => {
      res.status(400).send("unable to save to database");
    });
  // response object from api to client -------------
  res.json({ username: user, _id: userProfile._id })
})

// endpoint to get list of all users ---------------
app.get('/api/users', (req, res) => {
  userData.find({}, (err, arrOfUsers) => {
    if (err) console.log(err);
    res.json(arrOfUsers);
  })
})

// post for adding data to user profile ---------
app.post('/api/users/:_id/exercises', (req, res) => {
  
  let id = req.body[':_id'];
  let date = req.body.date ? req.body.date : null;
  //new Date().toDateString();
  let date2 = req.body.date ? new Date(date).toDateString() : new Date().toDateString();



 userData.findByIdAndUpdate(
    // 4 parameters
    // 1. id filter
    id,
    // 2. update
    { $push: { log: { 
        description: req.body.description,
        duration: parseInt(req.body.duration),
        date: date2
          }
       }
     },
    // 3. options
    { useFindAndModify: false, new: true },
    //  callback
    (err, result) => {
       if(err){
         console.error(err);
  } else if(result === null){
        res.send('Invalid input');
      } else {
    let resObj = {};
    resObj.username = result.username;
    resObj.description = req.body.description;
    resObj.duration = parseInt(req.body.duration);
    resObj._id = result._id;
    resObj.date = date2;

    //console.log(req.body);

    res.send(resObj);
      }
    }    
  // end findByIdAndUpdate ---------
 )
// end of app.post
})

app.get('/api/users/:_id/logs', (req, res)=>{
  //  variables
  let id = req.params._id;
  let arr = [];
  let from = req.query.from;
  let to = req.query.to;
  let limit = req.query.limit;
  
  userData.find({_id: id})
          .exec(function(err, result){
      if(err){
       console.log(err);
       res.send('Invalid ID');
    } else if(result === []){
      res.send('Invalid ID');
    } else if(result[0] && 
              from && 
              to && 
              limit && 
              typeof from === 'string' &&
              typeof to === 'string' &&
              typeof parseInt(limit) === 'number'
              ){
                // begin forEach 
                result[0].log.forEach((obj)=>{ 
                  if(Date(obj.date) >= Date(from) && 
                     Date(obj.date) <= Date(to) &&
                     arr.length < parseInt(limit) 
        
                     ){

                     arr.push(obj);

                    }
                })
                
              //end of forEach
              let resObj = {};
      resObj._id = result[0]._id;
      resObj.username = result[0].username;
      resObj.count = arr.length;
      resObj.log = arr;
      
      console.log('test1')
      res.send(resObj)

      } else if(result[0]){
        // begin forEach 
      result[0].log.forEach((obj)=>{
        arr.push(obj);
      })
        // end forEach
      let resObj = {};
      resObj._id = result[0]._id;
      resObj.username = result[0].username;
      resObj.count = arr.length;
      resObj.log = arr;


        console.log('test2')
        //console.log(result[0].log)
    
       res.send(resObj)
    // end of userData.find()
    }
  
  })
  // end of userData.find()
 })


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

Log all your route inputs and responses and you’ll find for the POST route, it fails with ‘Invalid Input’ every time.

which means the problem is here. Backing up, your find can only fail on a bad id, updat or filter. Since there is no error, that probably means your definition of id is bad (it is).

When I log from your GET route, I get

req.body: {}
req.params: {"_id":"61b92f67ce9ec9bb2d885d29"}
req.query: {"limit":"1"}
{
  _id: new ObjectId("61b92f67ce9ec9bb2d885d29"),
  username: 'fcc_test_16395262476',
  count: 2,
  log: [
    { description: 'test', duration: 60, date: 'Mon Jan 01 1990' },
    { description: 'test', duration: 60, date: 'Tue Jan 02 1990' }
  ]
}

The limit was 1, but 2 records were sent.

Thanks so much! I was able to figure it out this was very helpful!