简体   繁体   English

两个Date.now()之间的巨大时间间隔

[英]Gigantic time lapse between two Date.now()

SITUATION: 情况:

I have created a spam time-limit of 1 hour. 我创建了1小时的垃圾邮件时限。

When a user posts, the current time Date.now() is saved in user.last . 用户发布时,当前时间Date.now()保存在user.last

When he posts again, I get the current time with Date.now() and compare it to user.last . 当他再次发布信息时,我使用Date.now()获取当前时间,并将其与user.last进行比较。


PROBLEM: 问题:

The problem is the following: 问题如下:

I post twice in about 1 minute. 我在大约1分钟内发布了两次。

The difference of time between the two posts is greater than 1 HOUR according to my the console output of my code. 根据我的代码的控制台输出,两个帖子之间的时间差大于1小时。


QUESTION: 题:

What have I done wrong ? 我做错了什么? I did set the env TZ to UTC. 我确实将env TZ设置为UTC。


CODE: 码:

exports.create = function (req, res) {
  var article = new Article(req.body);
  article.user = req.user;

  console.log("LAST: "+article.user.last.getTime());
  console.log("Date.now(): "+Date.now());

  if (article.user.last.getTime() != null && article.user.last.getTime() != undefined) {
      console.log("DATE DIFFERENCE: "+(Date.now() - article.user.last));
      if ((Date.now() - article.user.last > 1000 * 60 * 60)) {
            article.save(function (err) {
                if (err) {
                  return res.status(422).send({
                    message: errorHandler.getErrorMessage(err)
                  });
                } else {
                    res.json(article);

                    if (article.user) {
                        article.user.last = Date.now();
                        console.log("NEW LAST: "+article.user.last.getTime());
                    } else {
                        res.status(401).send({
                          message: 'User is not signed in'
                        });
                    }
                }
            });
      }
      else {
          return res.status(422).send({
            message: "You need to wait 1 hour between Article creations or if you just created an account."
          });
      }
  }
  else {
      console.log("2");
      article.save(function (err) {
        if (err) {
          return res.status(422).send({
            message: errorHandler.getErrorMessage(err)
          });
        } else {
          res.json(article);

            if (article.user) {
                article.user.last = Date.now();
                console.log("3) LAST: "+article.user.last.getTime());
            } else {
                res.status(401).send({
                  message: 'User is not signed in'
                });
            }
        }
    });
  }
};

CONSOLE: 安慰:

LAST: 1486438249652
Date.now(): 1486515834377
DATE DIFFERENCE: 77584725
NEW LAST: 1486515834394

EDIT: 编辑:

User model 用户模型

/**
 * User Schema
 */
var UserSchema = new Schema({

  //SOME CODE

  last: {
    type: Date
  },

  //SOME CODE

});

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() ). 您的代码保存了Article,然后设置了用户的.last属性,但是对于要保存的用户而言,Mongoose模型绝不是( article.user.save() )。

That means .last remains whatever it was when it was first created, and so the logged date difference will increase monotonically. 这意味着.last保留其最初创建时的状态,因此记录的日期差异将单调增加。

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'});
    }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM