当我将**@nest/swagger**库更新到版本4时,发生了以下错误:
(node:16134) UnhandledPromiseRejectionWarning: Error: A circular dependency has been detected (property key: "customer"). Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").
at SchemaObjectFactory.createNotBuiltInTypeReference (/opt/desenvolvimento/Haizen/projectx_back/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:182:19)
at SchemaObjectFactory.mergePropertyWithMetadata (/opt/desenvolvimento/Haizen/projectx_back/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:117:25)
at /opt/desenvolvimento/Haizen/projectx_back/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:66:35
at Array.map (<anonymous>)
at SchemaObjectFactory.exploreModelSchema (/opt/desenvolvimento/Haizen/projectx_back/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:65:52)
at SchemaObjectFactory.createNotBuiltInTypeReference (/opt/desenvolvimento/Haizen/projectx_back/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:187:37)
at SchemaObjectFactory.mergePropertyWithMetadata (/opt/desenvolvimento/Haizen/projectx_back/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:117:25)
at /opt/desenvolvimento/Haizen/projectx_back/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:66:35
at Array.map (<anonymous>)
at SchemaObjectFactory.exploreModelSchema (/opt/desenvolvimento/Haizen/projectx_back/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:65:52)
我的模型类似乎这样:
@Entity()
export class Job {
.
.
.
@ManyToOne(type => Customer, customer => customer.jobs)
@ApiProperty({ type: Customer })
customer: Customer;
}
6条答案
按热度按时间zynd9foi1#
对我有效的解决方案是在**@ApiProperty()中使用箭头函数声明类型**,如下所示:
sc4hvdpw2#
至少还有三种情况会出现相同的错误消息,即使它们与双向关系无关:
枚举为类型
错:
正确答案:
匿名类型
错:
正确答案:
为空
错:
正确答案:
我在Github上为此创建了一个问题:https://github.com/nestjs/swagger/issues/1475
stszievb3#
对于同样有这个问题的人,你可以把
type
键改成@ApiProperty
上的enum
键。这对我很有效。sdnqo3pr4#
我最近也遇到了这个问题。在我的例子中,我在我的custom-validations文件夹的同一个文件中使用了几个验证器。错误“A circular dependency has been detected”只出现在
nest build
和node dist/main
之后。我不确定这是否是NestJs + Swagger在生成构建后的问题。显然,我的修复是将验证器放入几个文件中(每个文件包含一个验证器),它对我很有效。
jdzmm42g5#
我在实体的嵌套属性上使用type interfaces时遇到了这个问题
不正确:
Nest.js建议使用类来代替--即使是在嵌套属性上:
正确答案:
20jt8wwn6#
看看这个bugfix
我设法使我的枚举(ProcessCat)像这样工作:
因此NestJS可以正确编译。