简体   繁体   English

书架/ knex增加时间戳记时间

[英]bookshelf/knex add time to timestamp

Using NodeJS, Knex, Bookshelf JS ORM, and PostgreSQL, I have a field table.timestamp('reset_password_expires'); 使用NodeJS,Knex,Bookshelf JS ORM和PostgreSQL,我有一个字段表table.timestamp('reset_password_expires'); - how can I set the date for future (let's say 10 minutes from now)? -如何设定未来的日期(假设现在是10分钟)?

I've tried: 我试过了:

new User.User({'email': email}).fetch().then((user) => {
      user.set('reset_password_expires', Date.now() + 60000);
      user.save();
      ...

But this throws: 但这引发了:

Unhandled rejection error: date/time field value out of range: "1544577763253" 未处理的拒绝错误:日期/时间字段值超出范围:“ 1544577763253”

Also tried 也尝试过

user.set('reset_password_expires', 'NOW()' + '10 minutes'::interval);

But this is invalid syntax. 但这是无效的语法。

I've also tried changing field type to dateTime , with the same results. 我也尝试将字段类型更改为dateTime ,结果相同。

I think PostgreSQL requires timestamp to be on ordinary datetime format (as described here ). 我认为PostgreSQL的要求时间戳是在普通的日期时间格式(如描述在这里 )。 At the moment you are passing milliseconds since epoch. 目前,您距纪元已过去了毫秒。

In Developer Tools: 在开发人员工具中:

> Date.now() + 60000
1544706877041

You can try using this instead: 您可以尝试使用以下方法:

new Date((new Date()).getTime() + 60000)

In Developer Tools: 在开发人员工具中:

> new Date((new Date()).getTime() + 60000)
Thu Dec 13 2018 14:15:50 GMT+0100 (Central European Standard Time)

Here is a utility function that I use myself: 这是我自己使用的实用程序功能:

module.exports.inHours = (hours) => {
  const minutes = hours * 60;
  const seconds = minutes * 60;
  const milliseconds = seconds * 1000;

  return new Date((new Date()).getTime() + milliseconds);
};

My code looks slightly different than yours, but this is how I call the function: 我的代码看起来与您的代码略有不同,但这就是我调用该函数的方式:

await userQueries.update(user.user_id, {
  resetPasswordToken: resetToken,
  resetPasswordExpires: inHours(1),
  modified_at: new Date(),
});

This is a datetime, however, not a timestamp. 这是日期时间,但是不是时间戳。

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

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