首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sequelize-typescript 'HasManyCreateAssociationMixin‘不是函数

Sequelize-typescript 'HasManyCreateAssociationMixin‘不是函数
EN

Stack Overflow用户
提问于 2019-10-08 00:15:51
回答 1查看 650关注 0票数 3

我有一个模型是sequelize-typescript,Door.ts:

代码语言:javascript
复制
import { Table, Model, Column, AutoIncrement, PrimaryKey, ForeignKey, DataType, AllowNull, BelongsTo, HasMany } from 'sequelize-typescript';
import { Location } from '@modules/location';
import { AkilesServiceV1, AkilesServiceV0, IDoorService } from '@services/DoorService';
import { BelongsToGetAssociationMixin } from 'sequelize/types';
import { DoorLog } from '@modules/door_log';
import { HasManyCreateAssociationMixin } from 'sequelize';

@Table({ tableName: 'door' })
class Door extends Model<Door> {
    @PrimaryKey
    @AutoIncrement
    @Column
    id!: number;

    @AllowNull(false)
    @Column
    type!: string;

    @Column
    button_id!: string;

    @Column
    gadget_id!: string;

    @Column
    action_id!: string;

    @AllowNull(false)
    @Column(DataType.ENUM('vehicular','pedestrian'))
    access_type!: 'vehicular' | 'pedestrian';

    @AllowNull(false)
    @Column
    description_tag!: string;

    @Column(DataType.VIRTUAL)
    description!: string;

    @ForeignKey(() => Location)
    @AllowNull(false)
    @Column
    location_id!: number;

    @BelongsTo(() => Location)
    location!: Location;

    @HasMany(() => DoorLog)
    door_logs!: DoorLog[];

    public getLocation!: BelongsToGetAssociationMixin<Location>;
    public createDoorLog!: HasManyCreateAssociationMixin<DoorLog>;

    public async open () {
        let doorService: IDoorService;
        switch(this.type) {
            case 'akiles-v0':
                doorService = new AkilesServiceV0();
                break;
            case 'akiles-v1':
                doorService = new AkilesServiceV1();
                break;
            default:
                doorService = new AkilesServiceV1();
                break;
        }

        //await doorService.open(this);

        return await this.createDoorLog({ door_id: this.id, timestamp: new Date() });

    }

    public async getParking() {
        const location: Location = await this.getLocation();
        return await location.getParking();
    }
}

export default Door

正如您所看到的,它有两个与Mixins关联的函数:

代码语言:javascript
复制
public getLocation!: BelongsToGetAssociationMixin<Location>;
public createDoorLog!: HasManyCreateAssociationMixin<DoorLog>;

第一个可以像这样完美地工作:await this.getLocation()。但是,第二次调用它时,我是这样调用的:await this.createDoorlog ({door_id: this.id, timestamp: new Date ()})返回以下错误:

代码语言:javascript
复制
TypeError: this.createDoorLog is not a function

我也尝试过在不带参数的情况下调用函数,但得到了相同的结果。我不明白为什么这两个函数在创建时几乎相同,但行为却不同。我是不是错过了HasManyCreateAssociationMixin的某些东西?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2021-06-02 23:28:52

因为当我不可避免地再次遇到这个问题时,我会被同样的问题所困扰。答案是将"as“广告到@HasMany mixin。Sequelize似乎与camelcase类有问题。

所以在这种情况下,添加

代码语言:javascript
复制
@HasMany(() => DoorLog, options: {as: "doorLog" })
    door_logs!: DoorLog[];

或者类似的东西应该允许您使用这种混合

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58273415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档