【问题标题】:loader exception / circular reference by decorators加载器异常/装饰器的循环引用
【发布时间】:2019-04-14 11:21:59
【问题描述】:

问题

我得到一个循环加载程序异常。这可能是由编译器选项"emitDecoratorMetadata":true 引起的。 我该如何解决?感谢您提供有用的回放!

简介

我准备了一个最小项目来重现错误。请看一下我的临时 git 仓库:git repo for bug presentation

我使用两个库(typeormjson2typescript)并且都使用装饰器进行操作。我在某些类属性上使用了两个库中的多个装饰器。

复制步骤:

  1. 克隆 git 存储库。
  2. 通过命令npm i (npm 6.9.0) 安装所有包。
  3. Visual Studio Code打开根目录。
  4. 打开bugexample/test/test.spec.ts,进入调试视图,通过配置Mocha current file开始调试。

在这些步骤之后,您应该会看到异常输出。

/bugexample/node_modules/reflect-metadata/Reflect.js:553
                var decorated = decorator(target, propertyKey, descriptor);
                                ^
Error: Fatal error in JsonConvert. It is not allowed to explicitly pass "undefined" as second parameter in the @JsonProperty decorator.

        Class property: 
                banana

Use "Any" to allow any type. You can import this class from "json2typescript".

属性banana 获取类型Banana 作为参数,此类型为undefined,原因不明。库json2typescript 不是造成此问题的原因。


分析

现在我想分解这个问题。 我从两个模型类开始,以测试结束。

首先请看bug_presentation/src/persistence/models/ape.model.ts

import { JsonObject, JsonProperty } from "json2typescript";
import { Column, Entity, OneToOne, PrimaryGeneratedColumn } from "typeorm";

import { Banana } from "./banana.model";

/**
 * This is an entity class.
 * 
 * @author Tim Lehmann <l_@freenet.de>
 */
@JsonObject("Ape")
@Entity()
export class Ape {

  @PrimaryGeneratedColumn()
  readonly id: number

  @JsonProperty('0')
  @Column()
  readonly name: string = null

  // the associated table holds the foreign keys

  @JsonProperty('1', Banana)
  @OneToOne(type => Banana, banana => banana.possessionOf, { cascade: true })
  readonly banana = new Banana();
}

在第 24 行,类型 Banana 是传递的参数,但由于未知原因,此时当前测试的类型为 undefined

现在请看bug_presentation/src/persistence/models/banana.model.ts

import { JsonObject, JsonProperty } from "json2typescript";
import { Column, Entity, JoinColumn, OneToOne, PrimaryGeneratedColumn } from "typeorm";

import { Ape } from "./ape.model";

/**
 * @author Tim Lehmann <l_@freenet.de>
 */
@JsonObject("Banana")
@Entity()
export class Banana {

  @PrimaryGeneratedColumn()
  private readonly id: number

  @JsonProperty('0')
  @Column()
  readonly weight: string = null

  @OneToOne(type => Ape, possessionOf => possessionOf.banana)
  @JoinColumn({ name: "possessionOf" })
  readonly possessionOf: Ape = new Ape();
}

第 21 行和第 22 行有问题。如果我将这些行注释掉,则没有加载程序异常。

最后请看bug_presentation/test/test.spec.ts

import { expect } from "chai";

import { Ape } from "../src/persistence/models/ape.model";
import { Banana } from "../src/persistence/models/banana.model";

// const classApe = Ape;
const classBanana = Banana;

describe("check if class type exist", () => {

  it('check class Ape is defined', () => {
        // expect(classApe).exist;
  })

  it('check class Banana is defined', () => {
    expect(classBanana).exist;
  })
})

我想测试类型/类 Banana 是否未定义,但测试会提前中断,因为如果传递的属性(在本例中为 Banana)库 json2typescript 会引发异常,则为 @987654345 @。

奇怪的行为是,如果我将类 Ape 分配给一个变量(删除第 6 行的注释),那么类型/类 Banana 就被定义了。


【问题讨论】:

  • 如果您可以在最顶部总结问题/问题是什么,那将很有帮助。我开始阅读,但什至不确定我在寻找什么。

标签: node.js typescript commonjs typeorm reflect-metadata


【解决方案1】:

我知道我在这方面有点晚了,但我遇到了类似的问题并找到了这篇文章。经过一番挖掘,我意识到这根本不是 json2typescript 的问题,而是循环依赖问题。

我发现这篇非常有用的文章帮助我解决了项目中的问题:https://medium.com/visual-development/how-to-fix-nasty-circular-dependency-issues-once-and-for-all-in-javascript-typescript-a04c987cf0de

本质上,问题是:

  • ape.model.ts 你导入香蕉:import { Banana } from "./banana.model";
  • banana.model.ts你导入猿:import { Ape } from "./ape.model";

这些模块是串行加载的,所以当第一个模块被加载时(假设它的ape.model.ts)它会尝试导入香蕉 -> 香蕉被加载,它试图导入猿 -> 猿还没有完成加载所以它返回未定义。

我链接到的文章建议您创建一个internal.ts 文件来管理模块加载:

export * from './ape.model';
export * from './banana.model';

然后总是从内部加载,即在ape.model.ts:

import { Banana } from './internal';

【讨论】:

    猜你喜欢
    • 2020-12-04
    • 2020-01-10
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 2021-01-05
    • 2020-07-08
    • 2017-03-28
    • 2016-06-16
    相关资源
    最近更新 更多