Exercise Tracker: POST and GET user's exercise

Tell us what’s happening:
I do not know why I cannot pass the following tests

Here is my code

// Connect database
mongoose.connect(mongodb, { useNewUrlParser: true, useUnifiedTopology: true });

// Define Schema
let Schema = mongoose.Schema;
let userSchema = new Schema({
  username: {type: String, required: true},
  log: [{
    description: String,
    duration: Number,
    date: Date
  }]
});

// Create model
let userModel = mongoose.model('userModel', userSchema);

// Clear data in model
userModel.deleteMany({}, function(err, result){
  if(err) return handleError(err);
  console.log(result);
});

// POST user
app.post('/api/users', function(req, res){
  let userName = req.body.username;
  userModel.create({ username: userName }, function(err, result){
    if(err) {console.log(err)};

    console.log('POST user');
    console.log({
      username: result.username,
      _id: result._id
    });
    res.json({
      username: result.username,
      _id: result._id
    });
  });
});

// GET user
app.get('/api/users', function(req, res){
  userModel.find({}, '_id username __v', function(err, result){
    if(err) {console.log(err)};

    console.log('GET user');
    console.log(result);
    res.json(result);
  });
});

// POST exercise
app.post('/api/users/:_id/exercises', function(req, res){
  let userID = req.body[':_id'];
  let description = req.body.description;
  let duration = parseInt(req.body.duration);
  let date = new Date(req.body.date);
  let dateStr = date.toDateString();
  
  if(dateStr == 'Invalid Date'){
    date = new Date(Date.now());
    dateStr = date.toDateString();
  }

  let filter = { userID: userID };
  let log = {
    description: description,
    duration: duration,
    date: date
  }

  userModel.findOneAndUpdate(filter, { $push: { log: log} }, {new: true}, function(err, result){
    if(err)  {console.log(err)};

    console.log('POST exercise');
    console.log({
      _id: result._id,
      username: result.username,
      date: dateStr,
      duration: duration,
      description: description
    });
    res.json({
      _id: result._id,
      username: result.username,
      date: dateStr,
      duration: duration,
      description: description
    });
  });
});

// GET exercise
app.get('/api/users/:_id/logs', function(req, res){
  let userID = req.params._id;
  let dateFrom;
  let dateTo;
  let limit;

  let query = userModel.find({ _id: userID });
  req.query.from ? dateFrom = new Date(req.query.from) : dateFrom = new Date(0);
  req.query.to ? dateTo = new Date(req.query.to) : dateTo = new Date();

  query.exec(function(err, result){
    if(err){ console.log(err); }

    let newLog = [];
    for(let i = 0; i < result[0].log.length; i++){
      let newObj = {};
      newObj['description'] = result[0].log[i].description;
      newObj['duration'] = result[0].log[i].duration;

      if(!(dateFrom <= result[0].log[i]['date'] && result[0].log[i]['date'] <= dateTo)) {continue;}
      let dateStr = result[0].log[i]['date'].toDateString();
      newObj['date'] = dateStr;
      
      newLog.push(newObj);
    }
    
    if(req.query.limit){
      limit = parseInt(req.query.limit);
      newLog = newLog.slice(0, limit);
    }
    let resObj = {
      _id: result[0]._id,
      username: result[0].username,
    };
    if(req.query.from){
      resObj['from'] = dateFrom.toDateString()
    }
    if(req.query.to){
      resObj['to'] = dateTo.toDateString()
    }
    resObj['count'] = newLog.length;
    resObj['log'] = newLog;
    
    console.log('GET exercise');
    console.log( resObj );
    res.json( resObj );     
  });
});

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

Challenge: Exercise Tracker

Link to the challenge:

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