typescript - 创建实体实例导致 Reflect.getMetadata 不是函数

标签 typescript typeorm

我开始使用 typeorm。我创建了一些实体:

@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 可以很好地创建数据库 (Sqlite)。但是,每当我创建我的实体之一的实例时,例如 let = user = new User(),NodeJS 立即崩溃并显示以下堆栈跟踪:

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)); ^

类型错误:Reflect.getMetadata 不是函数 在 C:\Users\Chris\Documents\TypeORM - Kopie (2)\node_modules\typeorm\decorator\columns\PrimaryColumn.js:20:76 在 __decorate (C:\Users\Chris\Documents\TypeORM - Kopie (2)\entities\Sprint.js:5:110) 在对象。 (C:\Users\Chris\Documents\TypeORM - Kopie (2)\entities\Sprint.js:19:1) 在 Module._compile (module.js:541:32) 在 Object.Module._extensions..js (module.js:550:10) 在 Module.load (module.js:456:32) 在 tryModuleLoad (module.js:415:12) 在 Function.Module._load (module.js:407:3) 在 Module.require (module.js:466:17) 在需要时(内部/module.js:20:19) 当我删除创建新实例的行时,一切都恢复正常了。我曾尝试使用不同的 PrimaryKey 装饰器,例如 @PrimaryColumn("int", { generated: true }),但这没有帮助。

编辑:我的 tsconfig.json: { “版本”:“2.1”, “编译器选项”:{ “库”:[“es5”,“es6”], “目标”:“ES5”, “模块”:“commonjs”, "moduleResolution": "节点", “emitDecoratorMetadata”:真实的, “experimentalDecorators”:真实的, “源 map ”:真实的, “typeRoots”:[“node_modules/@types”] }, “排除”: [ “节点模块” ]}

非常感谢。

最佳答案

确保您使用的 TypeScript 编译器版本 > 2.1 并且您已在 tsconfig.json 中启用以下设置:

"emitDecoratorMetadata": true,
"experimentalDecorators": true

还要确保在使用 orm 的任何代码之前导入了 reflect-metadata shim:

import "reflect-metadata";

关于typescript - 创建实体实例导致 Reflect.getMetadata 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41203244/

相关文章:

typescript - 如何只输出我的 typescript 项目的 d.ts?

javascript - 将 json 响应数组的值映射到预定义接口(interface)

typescript - 生成两个具有不同装饰器选项的类

postgresql - 如何为 postgresql MATERIALIZED VIEW 创建 typeorm 实体

postgresql - TypeORM postgresql 急切加载关系定义为惰性

npm - 对 TypeScript 使用 "require(...)"的方式感到困惑

html - 如何在 Angular (点击)方法中包含数据层函数

Angular 国际化 (i18n) 和 *ngFor

javascript - NestJS/TypeORM : Cannot set property metadata of#<Repository> which has only a getter

node.js - 将 typeorm 中的 bigints 保存到 SQL Server 数据库 - 参数 '0' 的验证失败。值必须介于 -2147483648 和 2147483647 之间(含)