profile
viewpoint

Ask questionsgetDataValue and setDataValue does not exist

Versions <!-- Please let us know which sequelize, sequelize-typescript and typescript version are you using -->

  • sequelize: 5.9.3
  • sequelize-typescript: 1.0.0-beta.3
  • @types/sequelize: 4.28.3
  • typescript 3.5.2

I'm submitting a ... <!-- (check one with "x") --> [x] bug report [ ] feature request

Actual behavior:

@Column({
    type: DataType.STRING,
    get(): number[] {
        return this.getDataValue("results").split(",") as number[];
    },
    set(val: number[]): void {
        this.setDataValue("results", val.join(","));
    },
})
public results: number[];

Typescript say

Property 'getDataValue' does not exist on type 'Partial<ModelAttributeColumnOptions>'.
Property 'setDataValue' does not exist on type 'Partial<ModelAttributeColumnOptions>'.
RobinBuschmann/sequelize-typescript

Answer questions fjodor-rybakov

@fjodor-rybakov Thanks for sharing. Seems to be an issue with the types. I don't know why they assume the same type for the dataValues and what the getters return and the setters get passed in. (You could open an issue for that on https://github.com/sequelize/sequelize) So for now you can only fix this by using as any:

(this.getDataValue("results") as any).split(",");
this.setDataValue("results", value.join(",") as any);

or you could use the JSON data type and store the number array as it is:

    @Column(DataType.JSON)
    results(): number[];

Oh, thanks! Use JSON type was better

useful!
source:https://uonfu.com/
Github User Rank List