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.
You should use upsert to create new data. So I would start by adding some options.
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.
}));
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!
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:
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:
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 )
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.
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.