Typeorm 迁移因 `beforeInsert()` 失败

问题描述 投票:0回答:1

我正在进行一个 ESM 项目,其中有一个如下所示的数据库模型。我的最终目标是在创建应用程序后立即创建发行记录。

@Entity()
export class Application extends BaseEntity {
    ...other fields

    @AfterInsert()
    @AfterUpdate()
    async createAndLinkIssuance() {
        if (this.status === "approved") {
            const issuance = await CredentialIssuanceService.create({
                application: this,
                credentialId: this.credential.id
            });
            this.credentialIssuance = issuance;
        }
    }
}

CredentialIssuanceService.create
方法只是使用像这样的存储库模式在数据库中创建一个实体

async create(entity: DeepPartial<T>): Promise<T> {
        const entityCreate = this.repository.create(entity);
    await this.repository.save(entityCreate);
    return entityCreate;
}

当我运行此代码时,一切顺利,但当我尝试迁移时,它不希望迁移并给出错误

ReferenceError: Cannot access 'Application' before initialization
    at file:///Users/merul/Desktop/Projects/auvo/server/src/services/index.ts:7:53
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:431:15)

BeforeInsert
钩子似乎不想在这里工作,删除钩子可以修复错误。

我怎样才能解决这个问题,或者以其他方式达到相同的结果?

postgresql typeorm
1个回答
0
投票

似乎 typeorm 不喜欢挂钩中的 myservice,将其替换为存储库就可以了:)

© www.soinside.com 2019 - 2024. All rights reserved.