# Exercise Tracker - Add Exercise

**URL:** <https://forum.freecodecamp.org/t/exercise-tracker-add-exercise/263376>\
**Category:** General\
**Created:** [March 7, 2019, 10:45am UTC](https://forum.freecodecamp.org/t/exercise-tracker-add-exercise/263376 "2019-03-07T10:45:33Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![javineya](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/javineya/32/83511_2.png) [@javineya](https://forum.freecodecamp.org/u/javineya)\
**Post date:** [March 7, 2019, 10:45am UTC](https://forum.freecodecamp.org/t/exercise-tracker-add-exercise/263376/1 "2019-03-07T10:45:34Z")

</div>

I have tried many methods and done a lot of digging on this, but I simply don’t understand why I can’t add an exercise to my users. The users are created correctly, with an empty array for the exercise field, but I can’t add an exercise to that array.

Here’s my mongoose model:

```auto
const mongoose = require( 'mongoose' );

const userSchema = new mongoose.Schema({
  username: {
    type: String,
    required: true,
    unique: true
  },
  exercise: [{
    description: String,
    duration: Number,
    date: { 
      type: Date, 
      default: Date.now,
    }
  }]
});

const User = mongoose.model( 'etUser', userSchema );

module.exports = User;

```

Actually, the exercise array does _not_ give a default date, so that’s another issue, but I’m less concerned about that one at the moment.

And here’s my current iteration of an attempt to push onto that array:

```auto
app.post( "/api/exercise/add", ( req, res ) => {
  let data = req.body;
  let exercise = {
    description: data.description,
    duration: data.duration,
    date: data.date
  };
  
  dbUser.update(
    { _id: data.userId }, { 
    $push: { 
      exercise: { 
        description: data.description, 
        duration: data.duration
      } 
    }
  });
  
  res.send( exercise );
});

```

I’m only sending the exercise to get something back and to check that it’s sending what I want. It is.

Here’s the full code:

> **[Glitch](https://glitch.com/edit/#!/javineya-fcc-exercise-tracker)**
>
> Combining automated deployment, instant hosting & collaborative editing, Glitch gets you straight to coding so you can build full-stack web apps, fast

I appreciate your assistance in advance.

---

<div class="post-metadata">

**Author:** ![JP-Dub](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jp-dub/32/23213_2.png) [@JP-Dub](https://forum.freecodecamp.org/u/JP-Dub)\
**Post date:** [March 7, 2019, 3:22pm UTC](https://forum.freecodecamp.org/t/exercise-tracker-add-exercise/263376/2 "2019-03-07T15:22:26Z")

</div>

Hi,

You should use upsert to create new data. So I would start by adding some options.

```auto
 dbUser.update({_id: data.userId
    },{
    $push: {
      exercise : {
        foo: bar,
        bar: foo          
        }}
    }, {
      upsert: true, new: true
    }, ( (err, user) => {
      if(err) throw err;
      // the 'user' variable will just confirm the data was saved.
    }));

```

---

<div class="post-metadata">

**Author:** ![javineya](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/javineya/32/83511_2.png) [@javineya](https://forum.freecodecamp.org/u/javineya)\
**Post date:** [March 7, 2019, 3:30pm UTC](https://forum.freecodecamp.org/t/exercise-tracker-add-exercise/263376/3 "2019-03-07T15:30:21Z")

</div>

I’m dumb sometimes. I just came back here to update my own question because I realized I was missing the `callback function` and that’s why it wasn’t doing anything.

You’ve (correctly) got a callback in your suggestion, and I’ll add your proposed options as well. Thanks!

---

<div class="post-metadata">

**Author:** ![JP-Dub](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jp-dub/32/23213_2.png) [@JP-Dub](https://forum.freecodecamp.org/u/JP-Dub)\
**Post date:** [March 7, 2019, 3:32pm UTC](https://forum.freecodecamp.org/t/exercise-tracker-add-exercise/263376/4 "2019-03-07T15:32:13Z")

</div>

Hi don’t send the ‘user’ to the client - that info is just for confirmation.

---

<div class="post-metadata">

**Author:** ![javineya](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/javineya/32/83511_2.png) [@javineya](https://forum.freecodecamp.org/u/javineya)\
**Post date:** [March 8, 2019, 10:46am UTC](https://forum.freecodecamp.org/t/exercise-tracker-add-exercise/263376/5 "2019-03-08T10:46:12Z")

</div>

Thanks for the help. Now that I’ve got it saving and all that appears to work as intended, that minor issue I mentioned is turning out to be a hassle to correct.

I can’t seem to get a default `Date.now` to populate if the user doesn’t provide a date. I’ve tried setting a default option in my user model:

```auto
date: {
    type: Date,
    default: Date.now
}

```

This is the recommendation from Mongoose docs. I’ve tried inserting it with a ternary operator in my exercise variable:

```auto
let exercise = {
    description: data.description,
    duration: data.duration,
    date: date === null ? Date.now : data.date
}

```

And I’ve tried setting a function to fire in that same place:  
`date: defaultDate( data.date )`

```auto
const defaultDate = ( date ) => {
    if ( date === null ) {
        return Date.now // also tried 'new Date()'
    }
    
    return date
}

```

Nothing seems to work. If it manually enter a date in the field, it works fine, but the default behavior should use Date.now if no date is given.

I’m lost on this one; it doesn’t seem like it should be very complicated to do this.

---

<div class="post-metadata">

**Author:** ![eoja](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/eoja/32/135387_2.png) [@eoja](https://forum.freecodecamp.org/u/eoja)\
**Post date:** [July 2, 2019, 6:14pm UTC](https://forum.freecodecamp.org/t/exercise-tracker-add-exercise/263376/6 "2019-07-02T18:14:09Z")

</div>

In my variable I used `new Date()` rather than `Date.now` and it worked. (I also tried to use `Date.now` first but it set the date to null.) This is 4 months later, maybe you got it figured out already.

---

<div class="post-metadata">

**Author:** ![djma777](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/djma777/32/134674_2.png) [@djma777](https://forum.freecodecamp.org/u/djma777)\
**Post date:** [November 13, 2019, 3:11pm UTC](https://forum.freecodecamp.org/t/exercise-tracker-add-exercise/263376/7 "2019-11-13T15:11:55Z")

</div>

@JP-Dub,

Hi, thanks so much for your solution, it also solved my problem. I need to ask, where can I find `$push` in the mongoose docs?

---

<div class="post-metadata">

**Author:** ![JP-Dub](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jp-dub/32/23213_2.png) [@JP-Dub](https://forum.freecodecamp.org/u/JP-Dub)\
**Post date:** [November 13, 2019, 4:12pm UTC](https://forum.freecodecamp.org/t/exercise-tracker-add-exercise/263376/8 "2019-11-13T16:12:45Z")

</div>

Hi, I’m not sure if I found anything other than references to `$push` in the mongoose docs, but it’s there in the MongoDB documentation.

[https://docs.mongodb.com/manual/reference/operator/update/push/index.html](https://docs.mongodb.com/manual/reference/operator/update/push/index.html)

---

<div class="post-metadata">

**Author:** ![djma777](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/djma777/32/134674_2.png) [@djma777](https://forum.freecodecamp.org/u/djma777)\
**Post date:** [November 14, 2019, 6:14am UTC](https://forum.freecodecamp.org/t/exercise-tracker-add-exercise/263376/9 "2019-11-14T06:14:05Z")

</div>

Perfect! Thanks very much! @JP-Dub

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 11:34am UTC](https://forum.freecodecamp.org/t/exercise-tracker-add-exercise/263376/10 "2021-01-16T11:34:14Z")

</div>

Closed on January 16, 2021, 11:34am UTC


