0

I have a MEAN stack running on AWS Elastic Beanstalk. A common recurring bug is that Node will record an incorrect date value in the database when a user registers. I'm sure i can find a work around for this, but i'm just so puzzled as to why this would be happening seemingly randomly.

Here is a screenshot from the database showing multiple users with the exact same created_at date although they actually registered on different days and certainly different times. Most of the users shown in this screenshot registered between 11-17 and 11-25

enter image description here

The date is saved in Mongo class method when the user is being saved.

UserSchema.pre('save', function(next) {
  var currentDate = new Date();
  this.updatedAt = currentDate;
  if (!this.createdAt)
    this.createdAt = currentDate;

  next();
});

One thought is the way i have that field declared for the model using a default value. But i'm still not sure how that would explain it.

createdAt: { type: Date, default: new Date() },

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
TheValyreanGroup
  • 3,554
  • 2
  • 12
  • 30
  • That code will run every time any change is made. Don't use "hooks" for this. Use the [`timestamps`](https://mongoosejs.com/docs/guide.html#timestamps) option on the mongoose schema. – Neil Lunn Nov 25 '18 at 19:46
  • You probably copied the approach from [content like this](https://stackoverflow.com/a/12670523/2313887), which is **wrong** and the correct approach is actually a [different answer on the same question](https://stackoverflow.com/a/33242994/2313887). Don't just follow the first answer you see with a lot of votes, or anything from an external blog post, since it was probably copied from here anyway. As noted this is also in the mongoose documentation, which is the original link above. – Neil Lunn Nov 25 '18 at 19:52
  • @NeilLunn Yes, i may have used that (many months ago when this was built), but regarding your comment about 'every time a change is made', thats what the `if` is for. It should only store the `createdAt` value the first time the model is created. And either way, it doesn't explain how it could use the exact same date over and over for different stored objects. – TheValyreanGroup Nov 25 '18 at 20:02
  • There's two linked duplicates to your question in the box above it. One of them has an answer from me which shows how mongoose timestamp settings and other pre hooks are actually applied. Read them – Neil Lunn Nov 25 '18 at 20:04
  • @NeilLunn Those are not duplicates at all. They are solutions. I want to know _why_ the date is wrong and the same for multiple new records when that is almost impossible. If my question is not clear about that, then I will gladly edit. But it seems pretty clear to me. – TheValyreanGroup Nov 25 '18 at 23:01
  • Then if you want to know *"why"* then actually [read the content.](https://stackoverflow.com/questions/49863685/does-mongoose-upsert-operation-update-renew-default-schema-values) You "pre" hook is using `$set` instead of `$setOnInsert`, just as is described. So sorry, but this is not a new question. – Neil Lunn Nov 26 '18 at 00:20

0 Answers0