creating an instance of entity causes Reflect.getMetadata is not a function

11.5k views Asked by At

I started using typeorm. I have created some entities:

@Table()
export class User {
    @PrimaryColumn()
    name: string;
    @Column()
    passwordHash: string;
    @OneToMany(type => Sprint, sprint => sprint.scrumMaster)
    sprints: Sprint[];
    @OneToMany(type => BacklogItem, item => item.assignedTo)
    assignments: BacklogItem[];
    @OneToMany(type => BacklogItem, item => item.createdBy)
    createdItems: BacklogItem[];
}
@Table()
export class Sprint {
    @PrimaryGeneratedColumn()
    id: number;
    @Column("date")
    start: Date;
    @Column("date")
    end: Date;
    @ManyToOne(type => User, user => user.sprints)
    scrumMaster: User;
    @OneToMany(type => BacklogItem, item => item.sprint)
    items: BacklogItem[];
    @Column()
    isFinished: boolean;
}

Typeorm creates the database (Sqlite) just fine. However, whenever I do create an instance of one of my entities, for example let = user = new User(), NodeJS instantly crashes with the following stacktrace:

C:\Users\Chris\Documents\TypeORM - Kopie (2)\node_modules\typeorm\decorator\columns\PrimaryColumn.js:20
    var reflectedType = ColumnTypes_1.ColumnTypes.typeToString(Reflect.getMetadata("design:type", object, propertyName));
                                                                       ^

TypeError: Reflect.getMetadata is not a function
at C:\Users\Chris\Documents\TypeORM - Kopie (2)\node_modules\typeorm\decorator\columns\PrimaryColumn.js:20:76
at __decorate (C:\Users\Chris\Documents\TypeORM - Kopie (2)\entities\Sprint.js:5:110)
at Object.<anonymous> (C:\Users\Chris\Documents\TypeORM - Kopie (2)\entities\Sprint.js:19:1)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Module.require (module.js:466:17)
at require (internal/module.js:20:19) 

When I remove the line that creates the new instance, everything is fine again. I have tried to use different PrimaryKey decoraters like @PrimaryColumn("int", { generated: true }), but this does not help.

EDIT: My tsconfig.json:

{
    "version": "2.1",
    "compilerOptions": {
        "lib": [
            "es5",
            "es6"
        ],
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "sourceMap": true,
        "typeRoots": [
            "node_modules/@types"
        ]
    },
    "exclude": [
        "node_modules"
    ]
}

Thanks a lot in advance.

2

There are 2 answers

6
pleerock On

Make sure you are using TypeScript compiler version > 2.1 and you have enabled following settings in tsconfig.json:

"emitDecoratorMetadata": true,
"experimentalDecorators": true

Also make sure you have imported reflect-metadata shim before any of your code with orm:

import "reflect-metadata";
1
Christian S. On

The sample github.com/typeorm/typescript-example works fine for me. I have now copied my entire code into the example project and my issues are gone.