Skip to content Skip to sidebar Skip to footer

Gigantic Time Lapse Between Two Date.now()

SITUATION: I have created a spam time-limit of 1 hour. When a user posts, the current time Date.now() is saved in user.last. When he posts again, I get the current time with Date.n

Solution 1:

The problem is that the user record isn't actually being saved. Your code saves the Article, and then sets the user's .last property, but at no point is the Mongoose model for the user being saved (article.user.save()).

That means .last remains whatever it was when it was first created, and so the logged date difference will increase monotonically.

Here's the important bit:

article.save(function(err) {
  if (err) {
    res.status(422).send({message: errorHandler.getErrorMessage(err)});
  } else {
    if (article.user) {
      article.user.last = Date.now();
      article.user.save(function (err) {
        if (err) res.status(500).send({message: 'Database error'});
        else res.json(article);
      }
    } else {
      res.status(401).send({message: 'User is not signed in'});
    }
  }
});

Post a Comment for "Gigantic Time Lapse Between Two Date.now()"