【问题标题】:How to implement self referencing model on loopback 4 without getting a Circular dependency如何在没有循环依赖的情况下在环回 4 上实现自引用模型
【发布时间】:2020-02-10 15:39:36
【问题描述】:

我在我的环回 4 项目中创建了一个名为 Lot 的新模型。很多可以有子批次(Lot 的实例)。 我添加了与 cli 的关系,但是当我使用 LotLotControllerPOST 方法创建一个新的子集时,我收到了这个错误:

Unhandled error in POST /lots/1/lots: 500 Error: Circular dependency detected: controllers.LotLotController --> @LotLotController.constructor[0] --> repositories.LotRepository --> @LotRepository.constructor[1] --> repositories.LotRepository
    at ResolutionSession.pushBinding (/path/to/my/project/node_modules/@loopback/context/src/resolution-session.ts:251:13)
    at Function.enterBinding (/path/to/my/project/node_modules/@loopback/context/src/resolution-session.ts:97:13)
    at Function.runWithBinding (/path/to/my/project/node_modules/@loopback/context/src/resolution-session.ts:112:49)
    at Binding.getValue (/path/to/my/project/node_modules/@loopback/context/src/binding.ts:332:40)
    at RequestContext.getValueOrPromise (/path/to/my/project/node_modules/@loopback/context/src/context.ts:762:32)
    at RequestContext.get (/path/to/my/project/node_modules/@loopback/context/src/context.ts:572:17)
    at DefaultHasManyRepository.getter [as getTargetRepository] (/path/to/my/project/node_modules/@loopback/context/src/inject.ts:426:16)
    at DefaultHasManyRepository.create (/path/to/my/project/node_modules/@loopback/repository/src/relations/has-many/has-many.repository.ts:79:41)
    at LotLotController.create (/path/to/my/project/packages/server/src/controllers/lot-lot.controller.ts:68:43)
    at invokeTargetMethod (/path/to/my/project/node_modules/@loopback/context/src/invocation.ts:255:47)
    at value_promise_1.transformValueOrPromise.args (/path/to/my/project/node_modules/@loopback/context/src/invocation.ts:232:12)
    at Object.transformValueOrPromise (/path/to/my/project/node_modules/@loopback/context/src/value-promise.ts:270:12)
    at invokeTargetMethodWithInjection (/path/to/my/project/node_modules/@loopback/context/src/invocation.ts:227:10)
    at InterceptedInvocationContext.invokeTargetMethod (/path/to/my/project/node_modules/@loopback/context/src/invocation.ts:118:14)
    at targetMethodInvoker (/path/to/my/project/node_modules/@loopback/context/src/interceptor.ts:341:23)
    at value_promise_1.transformValueOrPromise.fn (/path/to/my/project/node_modules/@loopback/context/src/interceptor-chain.ts:175:14)

确实,Controller 对 LotRepository 有依赖关系,而 LotRepository 现在对自身也有依赖关系:

constructor(
    @inject('datasources.db') dataSource: DbDataSource,
    @repository.getter('LotRepository')
    protected lotRepositoryGetter: Getter<LotRepository>,
    @repository.getter('ItemRepository')
    protected itemRepositoryGetter: Getter<ItemRepository>,
  ) {
    super(Lot, dataSource);
    this.items = this.createHasManyRepositoryFactoryFor(
      'items',
      itemRepositoryGetter,
    );
    this.registerInclusionResolver('items', this.items.inclusionResolver);
    this.subLots = this.createHasManyRepositoryFactoryFor(
      'subLots',
      lotRepositoryGetter,
    );
    this.registerInclusionResolver('subLots', this.subLots.inclusionResolver);
  }

我怎样才能避免这种情况? 谢谢

【问题讨论】:

    标签: node.js database loopback loopback4


    【解决方案1】:

    您必须更改创建多关系的方式。试试这个:

      constructor(
        @inject('datasources.db') dataSource: DbDataSource,
        @repository.getter('ItemRepository')
        protected itemRepositoryGetter: Getter<ItemRepository>,
      ) {
        super(Lot, dataSource);
        this.items = this.createHasManyRepositoryFactoryFor(
          'items',
          itemRepositoryGetter,
        );
        this.registerInclusionResolver('items', this.items.inclusionResolver);
    
        this.subLots = this.createHasManyRepositoryFactoryFor(
          'subLots',
          Getter.fromValue(this);
        );
        this.registerInclusionResolver('subLots', 
        this.subLots.inclusionResolver);
      }
    

    (您必须从 @loopback/core 导入的 Getter)。

    【讨论】:

      猜你喜欢
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      • 2020-11-11
      相关资源
      最近更新 更多