gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:56:24 27 4
gpt4 key购买 nike

我开始使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com