updatedAt我的表中有一个User类型的列Date,它存储日期和时间。我想仅使用日期而不是日期时间进行查询。我正在使用sequelizeSequelize.Op.gt和Sequelize.Op.lt运算Users符通过添加24 * 60 * 60 * 1000. 但日期不会增加一天。当我尝试减法时,它工作得完美无缺。我只能在使用 getTime() 方法后添加一天。
我很困惑为什么它在不使用减法时有效getTime()但在加法时不起作用。有人能解释一下吗?
长话短说
这有效:
[Sequelize.Op.gt]: new Date(new Date(updatedAt) - 24 * 60 * 60 * 1000)
//updateAt: 2018-11-27
//output: 2018-11-26
Run Code Online (Sandbox Code Playgroud)
这不起作用:
[Sequelize.Op.gt]: new Date(new Date(updatedAt) + 24 * 60 * 60 * 1000)
//updateAt: 2018-11-27
//output: 2018-11-27
Run Code Online (Sandbox Code Playgroud)
这有效:
[Sequelize.Op.lt]: new Date(new Date(updatedAt).getTime() + 24 * 60 * 60 * 1000)
//updateAt: 2018-11-27 …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个钩子beforeBulkUpdate并获取字段的新值和先前值。我尝试.reload()在模型上使用,_previousDataValues但它们仅适用于实例,而不适用于批量创建和更新(我正在使用批量更新)。有没有办法使用beforeBulkUpdate钩子获取字段的先前值?
beforeBulkUpdate: (person) => {
console.log(person.name) // 'John' (new name)
Person.findOne({ where: { id: input.id } }).then(person => {
person.reload().then(() => {
console.log(person.name) // 'John' (expected old name, but returns new name)
})
})
}
Run Code Online (Sandbox Code Playgroud)