【问题标题】:updating datetime value in Table using Postgres & Knex使用 Postgres 和 Knex 更新表中的日期时间值
【发布时间】:2019-07-18 20:34:54
【问题描述】:

我想将“访问令牌”到期日期时间更新到“用户”表中,以检查访问令牌是否有效以重置密码。 所以我正在使用时刻传递一个日期时间字符串值。

但它无法更新日期时间,没有错误消息。

“forgotPassword”功能无需更新“resetPasswordExpires”即可找到。

我尝试将“resetPasswordExpires”列类型设置为时间戳。它没有用。

[ Front-End ]
const inOneHour = moment()
  .add(1, 'hours')
  .format('MMMM Do YYYY, HH:mm:ss');


[ users table / Database ]
table.datetime('resetPasswordExpires', { useTz: false });
  exports.forgotPassword = (req, res) => {
   const { username, email, inOneHour } = req.body;

   return knex('users')
    .where({ username, email })
    .first()
    .then(async user => {
      if (user) {
        const token = await util.getRandomToken(user);
        const mailOptions = {
          ...
        };

        return knex('users')
          .where({ username })
          .first()
          .update({
            resetPasswordToken: token,
            resetPasswordExpires: inOneHour,
          })
          .then(() => {
            sendEmail(mailOptions)
              .then(() => {
                ...
              })
              .catch(err => {
                ...
              });
          });
      }
      ...
    });
};

谢谢!!

【问题讨论】:

    标签: postgresql knex.js


    【解决方案1】:

    嗯,我确信它失败只是因为你没有捕捉到异常。

    首先为 KNEX 添加错误处理程序

    return knex('users')
              .where({ username })
              .first()
              .update({
                resetPasswordToken: token,
                resetPasswordExpires: inOneHour,
              })
              .then(() => {
                // Do your work
              })
              .catch((err) => {
                // print out the error
              });
    

    现在解决问题,postgres 不支持您的日期格式,即.format('MMMM Do YYYY, HH:mm:ss');,因此您将无法使用这种格式。 您当前的格式会产生类似于 July 18th 2019, 19:03:41

    的输出

    您可以使用.format('MMMM DD YYYY, HH:mm:ss'); 作为初学者,此创建日期为July 18 2019, 19:03:41。但我建议您只使用.format(),这样您就不必担心每次在代码中使用它时的格式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-04
      • 2023-03-07
      • 2021-05-05
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      相关资源
      最近更新 更多