我想在一个MySql表中查找在某个日期范围内创建的所有记录。
于是我写道:
import { Sequelize, Model, DataTypes, Op } from 'sequelize';
const sequelize = new Sequelize({
// some db connection config
dialect: 'mysql'
})
class Patient extends Model {
public guid!: number;
public name!: string;
public recordState: number = 0;
public createdAt?: Date;
public updatedAt?: Date
}
Patient.init({
guid: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false
},
name: { type: DataTypes.STRING, allowNull: false },
recordState: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0
},
createdAt: DataTypes.DATE,
updatedAt: DataTypes.DATE
}, {
sequelize,
modelName: 'Patient',
timestamps: false
})
Patient.findAll({
where: {
createdAt: {
[Op.between]: [new Date('2020-02-02'), new Date()]
}
}
})
但是,当我尝试用tsc
编译它时,它报告了如下错误:
sequelize.ts:50:5 - error TS2322: Type '{ [between]: Date[]; }' is not assignable to type 'string | number | boolean | WhereAttributeHash | AndOperator | OrOperator | Literal | Where | Fn | Col | WhereOperators | Buffer | WhereGeometryOptions | (string | ... 2 more ... | Buffer)[]'.
Types of property '[Op.between]' are incompatible.
Type 'Date[]' is not assignable to type 'string | number | boolean | [number, number] | WhereAttributeHash | AndOperator | OrOperator | Literal | Where | ... 5 more ... | (string | ... 2 more ... | Buffer)[]'.
Type 'Date[]' is not assignable to type '(string | number | WhereAttributeHash | Buffer)[]'.
Type 'Date' is not assignable to type 'string | number | WhereAttributeHash | Buffer'.
Type 'Date' is not assignable to type 'WhereAttributeHash'.
Index signature is missing in type 'Date'.
50 createdAt: {
~~~~~~~~~
Found 1 error.
看起来我不能用带日期范围的Op.between
,但是我用JS写了类似的代码就可以了。
所以我想知道我的TS代码中是否真的有问题,或者只是类型定义中缺少了一个,或者不推荐使用带日期的Op.between
?
2条答案
按热度按时间0sgqnhkj1#
您正在传递一个日期对象而不是字符串。请执行以下操作:
sqxo8psd2#
在我情况下,我有:
并努力变更为: