Log for User Exercises in Exercise Tracker not updating properly

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

On adding exercises for a user in Mongodb the exercise log is not updated properly using updateOne. Although it add the exercise but both the description and duration does not show up, but the date does. I tried to console.log() and it’s there. I am new to MongoDB and this part is the problem here.

  app.route('/api/users/:_id/exercises')
 .post((req,res)=>{
  const description = req.body.description;
  const duration = req.body.duration;
  const date = req.body.date;
  const id = req.params.id;
  console.log("description : --> ", description);
  console.log("duration : --> ", duration);
 
  User.updateOne({id:id}, 
                {$inc: {count:1},
                 $set: {
                    $push: {
                      log : { $each: {
                        'log.date': date,
                        'log.duration' : duration,
                        'log.description' : description
                      }
                      }
                    }
                  }
                },
    (err,data)=>{
      if (err) return err;
    }
 );

 User.findOne({id:id},(err, data)=>{
    if (err) return err;
    console.log("exercises ", JSON.stringify(data));
    res.json(data);      
  }); });

Below is the sample output and you can see the log has data on it but only _id and date are populated.

exercises {"_id":“623462f498222a6740026836”,“username”:“fcc_test_16476003717”,"__v":0,“count”:160,“log”:[{"_id":“62355ac242588a32391f5b8b”,“date”:“2022-03-19T04:23:30.486Z”}]}

My schema by the way is a little bit different since I embedded exercise in User as log.

Thanks for any help.

GOD bless.

Your project link(s)

solution: boilerplate-project-exercisetracker - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62

Challenge: Exercise Tracker

Link to the challenge:

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