Reputation: 756
I create a new model named Lot
on my loopback 4 project. A lot can have sublots (that are instances of Lot
).
I added a relation with the cli, but when I create a new sublot with the POST
method of LotLotController
I get this error:
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)
Indeed, the Controller has a dependency to the LotRepository
that now has a dependency to itself:
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);
}
How can I avoid this ? Thanks
Upvotes: 2
Views: 1045
Reputation: 46
You have to change the way you create the has many relation. Try this :
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);
}
(the Getter you must import from @loopback/core).
Upvotes: 3