Sequelize 返回日期 -1 天。例如,如果我在 WHERE 中执行带有日期的 select 语句:
“日期”:“2017-01-01”
它将返回:
'2016-12-31'
。
其他一切都会正确。应与 Sequelizes 语句匹配的 SQL Select 语句也显示正确的信息。时区匹配..
router.post("/perUser", async (req, res) => {
Labor.findAll({
where: {
clock: req.body.clock,
date: req.body.date,
entered_by: req.body.entered_by
}
})
.then(labor => {
console.log(labor);
})
.catch(err => console.log(err));
});
发送的数据:
{
"clock":"000072",
"date":"2017-01-01",
"entered_by":"username removed"
}
回应:
{
id: 1272,
clock: '000072',
name: 'removed',
rate: 13,
swo: 'removed',
cust_no: 'removed',
customer: 'removed',
wcn: 'removed',
hours: '1',
labor_dollars: 13,
burden: 23.4,
entered_by: 'removed',
team_no: '14',
date: '2016-12-31',
decHours: null
}
所以我猜测在 Sequelize 或 Node 中缺少一个设置或某些东西,但谷歌没有发现任何结果......任何真正的帮助将不胜感激。
最佳答案
您可以在模型定义中为日期列定义 getter:
date: {
type: DataTypes.DATE,
allowNull: true,
get() {
const date = this.getDataValue('date') ? moment(this.getDataValue('date')).format('YYYY-MM-DD') : null;
return date;
}
}
关于Node.js Sequelize 返回日期减去 1 天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60762910/