Sequelize V5->V6 升级:Typescript 错误属性 'x' 在类型 'y' 上不存在
Posted
技术标签:
【中文标题】Sequelize V5->V6 升级:Typescript 错误属性 \'x\' 在类型 \'y\' 上不存在【英文标题】:Sequelize V5->V6 upgrade: Typescript Error Property 'x' does not exist on type 'y'Sequelize V5->V6 升级:Typescript 错误属性 'x' 在类型 'y' 上不存在 【发布时间】:2020-12-17 10:40:23 【问题描述】:我将从 sequelize 5.x.x 升级到 6.x.x。 在 sequelize v5 中一切正常,但是当我升级时,当我想使用包含中的对象时,通过关联生成的所有属性都会出现打字稿错误。 我还尝试了https://sequelize.org/master/manual/typescript.html 中的示例,但没有成功。
希望有人能帮帮我!!
型号: Participant.js
import Model, DataTypes from 'sequelize';
import STATUS from '@bb/shared-core/dist/constants/participant';
import sequelize from '@bb/db';
import User from '@bb/components/erp/v1/modules/users/models/User';
import OrderItem from '@bb/components/ecommerce/v2/modules/orders/models/OrderItem';
import Contact from '@bb/components/erp/v1/modules/contacts/models/Contact';
import DeserializedModel from '../interfaces/Participant';
export class Participant extends Model implements DeserializedModel
id!: number;
fkUser: number;
fkOrderItem: number;
fkContact: number;
adminNotes: string;
trainerNotes: string;
attendance: string;
status: string;
readonly createdAt: Date;
readonly updatedAt: Date;
Participant.init(
fkUser:
type: DataTypes.NUMBER,
field: 'fk_user'
,
fkOrderItem:
type: DataTypes.NUMBER,
field: 'fk_order_item'
,
fkContact:
type: DataTypes.NUMBER,
field: 'fk_contact'
,
trainerNotes:
type: DataTypes.STRING,
field: 'trainer_notes'
,
adminNotes:
type: DataTypes.STRING,
field: 'admin_notes'
,
attendance:
type: DataTypes.ENUM('confirmed', 'unconfirmed'),
field: 'attendance'
,
status:
type: DataTypes.ENUM(STATUS.REBOOKED, STATUS.CANCELLED, STATUS.PARTICIPATING),
field: 'status',
defaultValue: 'participating'
,
sequelize,
modelName: 'participant'
);
Participant.belongsTo(User,
as: 'user',
foreignKey: 'fk_user',
targetKey: 'id'
);
Participant.belongsTo(OrderItem,
as: 'orderItem',
foreignKey: 'fk_order_item',
targetKey: 'id'
);
OrderItem.hasMany(Participant,
as: 'participants',
foreignKey: 'fk_order_item',
sourceKey: 'id'
);
Participant.belongsTo(Contact,
as: 'contact',
foreignKey: 'fk_contact',
targetKey: 'id'
);
if (process.env.NODE_ENV === 'test')
Participant.sync();
export default Participant;
这是使用该模型的函数: ParticipantService.js
private async getEventIdFromParticipant(participantId: number): Promise<number|undefined>
const participant = await Participant.findOne(
where:
id: participantId,
,
include: [
model: OrderItem,
as: 'orderItem',
]
);
return participant?.orderItem?.fkEvent;
错误在返回语句中的ParticipantService.js中。
Property 'orderItem' does not exist on type 'Participant'
【问题讨论】:
【参考方案1】:你也需要在类中定义它:
import Model, DataTypes, BelongsTo from 'sequelize';
export class Participant extends Model implements DeserializedModel
id!: number;
fkUser: number;
fkOrderItem: number;
fkContact: number;
adminNotes: string;
trainerNotes: string;
attendance: string;
status: string;
public readonly orderItem?: OrderItem;
public static associations:
orderItem: BelongsTo<Participant, OrderItem>;
;
readonly createdAt: Date;
readonly updatedAt: Date;
【讨论】:
以上是关于Sequelize V5->V6 升级:Typescript 错误属性 'x' 在类型 'y' 上不存在的主要内容,如果未能解决你的问题,请参考以下文章
想要将项目从 Angular v5 升级到 Angular v6
React Router v6 和 ow Params 不像 v5 那样工作