【问题标题】:Using Entitiy Repository in TypeORM Subscriber在 TypeORM 订阅者中使用实体存储库
【发布时间】:2021-01-11 12:30:26
【问题描述】:

我想在我的“AuthSubscriber”中使用“Promo”实体存储库。

这样做的原因是我想在创建“用户”实体时自动生成一个“促销”实体并将其分配给“用户”实体。

尝试在订阅者类中初始化存储库时,我收到以下错误:

RepositoryNotFoundError: No repository for "Promo" was found. Looks like this entity is not registered in current "default" connection?

订阅者:

@EventSubscriber()
export class AuthSubscriber implements EntitySubscriberInterface<User> {
  promoRepo: Repository<Promo>;

  constructor() {
    this.promoRepo = getRepository(Promo);
  }

  listenTo() {
    return User;
  }
}

Ormconfig:

{
  ...
  "logging": false,
  "synchronize": true,
  "entities": ["src/entities/**/*.ts"],
  "subscribers": ["src/subscribers/**/*.ts"]
}

【问题讨论】:

    标签: typeorm


    【解决方案1】:

    得知我可以通过以下方式访问存储库:

    const userRepo = event.manager.getRepository(User);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-29
      • 2022-10-13
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多