node.js - 如何在没有循环依赖的情况下在环回 4 上实现自引用模型
问题描述
我创建了一个以Lot
我的环回 4 项目命名的新模型。很多可以有子批次(即 的实例Lot
)。我添加了与 cli 的关系,但是当我使用 的POST
方法创建一个新的子集时,LotLotController
我得到了这个错误:
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
现在对自身具有依赖关系:
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);
}
我怎样才能避免这种情况?谢谢
解决方案
你必须改变你创建有很多关系的方式。尝试这个 :
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)。
推荐阅读
- php - iis 10 奇怪的行为 http 与 https - php 刷新 - responseBufferLimit 为 0
- javascript - 在 wordpress 中使用拉伸行和内容、拉伸行和内容(无填充)时,猫头鹰 Carousel2 图像不显示全宽
- javascript - Firebase React Web 应用程序 - 返回 [Object Promise] 而不是值,但在控制台中显示值
- java - 到达 RecyclerView 底部时从 API 接收下一页电影?
- javascript - 如何防止用户向按钮发送垃圾邮件?
- spring-batch - Spring Batch:JdbcPagingItemReader 分页
- javascript - 如果 cy.visit status 返回 404 状态代码,请重试不同的 url
- ios - 如果我的应用程序的字符串数据类型过多,它会获得 App Store 的批准吗?
- c# - 我应该为我的 .Net Core Web 应用程序使用 IMemoryCache 还是自定义 GlobalCache?
- amazon-web-services - S3 需要哪些权限才能允许部署到 AWS Amplify