typescriptexpresssequelize.jssequelize-typescript

TypeError in sequelize-typescript model


I am defining a typescript model for an express api using the Model Definition documentation for the module seqeulize-typescript.

sequlize-typescript

This is a new api being built so it is the only model at present. When starting the server I hit a TypeError: Cannot convert undefined or null to object for the first column in the model during the server start.

[nodemon] 2.0.22
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node dist/app.js`

Here is where the error is hit.

Here is the Model (I've excluded some column's as it is all the same concept.

import { Table, Model, Column, DataType } from 'sequelize-typescript';

@Table({
    timestamps: false,    
    tableName:"BranchNew",
    freezeTableName: true,
    modelName:"BranchNew",
    schema: 'hr',
})
export class Branch extends Model {
    @Column({
        type: DataType.INTEGER,
        allowNull: true,
    })
    ContractorCode!: number;
    @Column({
        type: DataType.INTEGER,
        autoIncrement: false,
        allowNull: false,
        primaryKey: true,
    })
    BranchNumber!: number;
    @Column({
        type: DataType.STRING,
        allowNull: false,
    })
    BranchName!: string;
    @Column({
        type: DataType.DATE,
        allowNull: true,
    })
    DateAcquired!: Date;
    @Column({
        type: DataType.STRING,
        allowNull: true,
    })
    Telephone!: string;
    ...
}

The error shows in the generated dist folder on this line. (I have split over lines to make it easier to read).

__esDecorate(null, null, _ContractorCode_decorators, 
{ kind: "field", name: "ContractorCode", static: false, private: false, 
access: { has: obj => "ContractorCode" in obj, 
get: obj => obj.ContractorCode, 
set: (obj, value) => { obj.ContractorCode = value; } } }, 
_ContractorCode_initializers, _instanceExtraInitializers);

All other columns appear afterwards with this (null, null,...) at the start. I assume my model is not defined correctly but it looks like it matches the docs mentioned above.


Solution

  • I went back and reread the documentation and I missed a message under the installation steps. It was quite clear now I have gone back. The docs listed in the question show this below installation.

    Your tsconfig.json needs the following flags:

    "target": "es6", // or a more recent ecmascript version
    "experimentalDecorators": true, * I still had this commented
    "emitDecoratorMetadata": true * I still had this commented
    

    So if facing the same issue, uncomment these 2 lines and it should build. The error lines no longer appear in the [model].js of the dist folder.