Sequelize-typescript: createdAt and updatedAt(timestamps) are undefined when building a model using build method.

Created on 8 Mar 2019  路  10Comments  路  Source: RobinBuschmann/sequelize-typescript

async getSequelizeObj<T extends Model<T>>(modelType: any, key: string): Promise<T> {
        // Getting the value from redis as string and the parsing it.
        const value = await this._get(this.__getFullKey(key));
        return modelType.build(JSON.parse(value), {
            isNewRecord: false
        });
    }

Here I am passing the value as JSON string which I am then parsing and the value is the serialised string of one of my models of sequelize but I am getting createdAt and updatedAt as undefined here.

Most helpful comment

Related to https://github.com/RobinBuschmann/sequelize-typescript/issues/560

This should fix it:

        return modelType.build(JSON.parse(value), {
            isNewRecord: false,
            raw: true
        });

All 10 comments

How does your model code look?

// Imports omitted
@Table({
    timestamps: true,
    paranoid: true,
    tableName: "videos"
})
export class Video extends Model<Video> {

    @Unique
    @PrimaryKey
    @AutoIncrement
    @Column(DataType.INTEGER.UNSIGNED)
    id: number;

    @Column(DataType.STRING)
    title: string;

    @Column(DataType.STRING)
    namespace: string;

    @Column(DataType.INTEGER.UNSIGNED)
    channel_id: number;

    @Column(DataType.INTEGER.UNSIGNED)
    owner_id: number;

    @Column(DataType.INTEGER)
    likes: number;

    @Column(DataType.INTEGER)
    views: number;
}

@kush3107 does value (the parsed json object) have a value for createdAt and updatedAt?

@RobinBuschmann yes it has. Let me know if you need more details from my end. Thanks

I don't know if this would help with anything relating to your work: https://stackoverflow.com/questions/17546953/cant-access-object-property-even-though-it-exists-returns-undefined
But perhaps try to define createdAt and updatedAt on your model? I read the second comment (by ramin) and thought it might be what's going on here.

@Table({
    timestamps: true,
    paranoid: true,
    tableName: "videos"
})

This annotation on the model class already adds the createdAt and updatedAt to the instances of the class and I can use them perfectly anywhere else. I am getting them undefined when I build the object from JSON using the build method.

I realize this, but I'm curious on the getter functionality. It was just a suggestion. If you add them manually, does it work? It's just a test-case to see if that's actually the issue or not.

Screenshot 2019-03-12 at 12 15 53 PM

I tried that also but didn't work. In the above screen you can see the JSON string that I am getting from cache and the model after the build is called.

Related to https://github.com/RobinBuschmann/sequelize-typescript/issues/560

This should fix it:

        return modelType.build(JSON.parse(value), {
            isNewRecord: false,
            raw: true
        });

Yep it worked. Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

libvirtadept picture libvirtadept  路  4Comments

oscarcalvo picture oscarcalvo  路  3Comments

bschveitzer picture bschveitzer  路  5Comments

zebkailash picture zebkailash  路  4Comments

fareshan picture fareshan  路  4Comments