gpt4 book ai didi

mysql - 在带有日期的 Sequelize 中使用 Op.between 时出现 TypeScript 错误

转载 作者:行者123 更新时间:2023-12-03 22:16:43 26 4
gpt4 key购买 nike

我想查找某个日期范围内创建的 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

最佳答案

您传递的是日期对象而不是字符串。这样做:

Patient.findAll({
where: {
createdAt: {
[Op.between]: [new Date('2020-02-02').toISOString(), new Date().toISOString()]
}
}
})

关于mysql - 在带有日期的 Sequelize 中使用 Op.between 时出现 TypeScript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60983696/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com