我想将“访问 token ”到期日期时间更新到“用户”表中,以检查访问 token 是否有效以重置密码。 所以我使用 moment 传递了一个日期时间字符串值。
但它无法更新日期时间且没有错误消息。
“忘记密码”功能可以在不更新“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 => {
...
});
});
}
...
});
};
谢谢!!
最佳答案
好吧,我确信它失败了,只是因为你没有捕捉到异常。
首先为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');
,此创建日期为 2019 年 7 月 18 日, 19:03:41
。但我建议您只执行 .format()
,这样您每次在代码中使用它时都不必担心格式。
关于postgresql - 使用 Postgres 和 Knex 更新表中的日期时间值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57069442/