0

我正在使用这个美妙的库:https ://www.npmjs.com/package/ts-mixer

整个回溯如下所示:


(node:22654) UnhandledPromiseRejectionWarning: Error: A circular dependency has been detected (property key: "firstName"). Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").
    at SchemaObjectFactory.createNotBuiltInTypeReference (/Users/albert/Documents/projects/albert/rlx/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:212:19)
    at SchemaObjectFactory.mergePropertyWithMetadata (/Users/albert/Documents/projects/albert/rlx/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:143:25)
    at /Users/albert/Documents/projects/albert/rlx/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:79:35
    at Array.map (<anonymous>)
    at SchemaObjectFactory.extractPropertiesFromType (/Users/albert/Documents/projects/albert/rlx/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:78:52)
    at SchemaObjectFactory.exploreModelSchema (/Users/albert/Documents/projects/albert/rlx/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:92:41)
    at /Users/albert/Documents/projects/albert/rlx/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:33:36
    at Array.map (<anonymous>)
    at SchemaObjectFactory.createFromModel (/Users/albert/Documents/projects/albert/rlx/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:20:45)
    at exploreApiParametersMetadata (/Users/albert/Documents/projects/albert/rlx/node_modules/@nestjs/swagger/dist/explorers/api-parameters.explorer.js:33:55)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:22654) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:22654) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

注册通用用户.dto.ts

export class RegisterCommonUserDto {
    @decorate(ApiProperty())
    @decorate(IsNotEmpty())
    @decorate(IsString())
    firstName: string;

    // @decorate(ApiProperty())
    @decorate(IsNotEmpty())
    @decorate(IsString())
    lastName: string;

    // @decorate(ApiProperty({enum: EUserRoleName}))
    @decorate(IsNotEmpty())
    @decorate(IsEnum(EUserRoleName))
    roleName: EUserRoleName;

    // @decorate(ApiProperty())
    @decorate(IsNotEmpty())
    @decorate(IsPhoneNumber())
    phoneNumber: string;

    // @decorate(ApiProperty())
    @decorate(IsNotEmpty())
    @decorate(IsEmail())
    email: string;
}

注册用户.dto.ts

export class RegisterUserDto extends Mixin(
    RegisterFighterDto,
    RegisterCommonUserDto,
    RegisterLocationProviderDto,
) {}

方法:

    @Post('register')
    public async registerUser(@Body() registerUserDto: RegisterUserDto): Promise<any> {
        // code
    }

什么可能导致问题?它肯定与@ApiProperty被包裹的装饰器有关decorate,因为当我注释掉该行时,错误消失了。但在那种情况下,装饰器不会被继承,这就是使用 ts-mixer 库的全部意义所在。

编辑:

有任何想法吗?

编辑:

这些库是不兼容的还是什么?

4

1 回答 1

1

最近,我学习 Nest 在尝试使用 Swagger 时也遇到了同样的问题。这是我的整个回溯看起来如下:

Error: A circular dependency has been detected (property key: "user"). Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").
at SchemaObjectFactory.createNotBuiltInTypeReference (/Users/wujie/Code/realworld/nestjs-realworld-example-app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:170:19)
at SchemaObjectFactory.createSchemaMetadata (/Users/wujie/Code/realworld/nestjs-realworld-example-app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:277:25)
at SchemaObjectFactory.mergePropertyWithMetadata (/Users/wujie/Code/realworld/nestjs-realworld-example-app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:122:21)
at /Users/wujie/Code/realworld/nestjs-realworld-example-app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:79:35
at Array.map (<anonymous>)
at SchemaObjectFactory.extractPropertiesFromType (/Users/wujie/Code/realworld/nestjs-realworld-example-app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:78:52)
at SchemaObjectFactory.exploreModelSchema (/Users/wujie/Code/realworld/nestjs-realworld-example-app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:92:41)
at /Users/wujie/Code/realworld/nestjs-realworld-example-app/node_modules/@nestjs/swagger/dist/swagger-explorer.js:217:64
at Array.forEach (<anonymous>)
at SwaggerExplorer.registerExtraModels (/Users/wujie/Code/realworld/nestjs-realworld-example-app/node_modules/@nestjs/swagger/dist/swagger-explorer.js:217:21)

分析错误代码信息,发现问题出在装饰器ApiExtraModels()上,我写的代码是:

import { applyDecorators, Type } from '@nestjs/common';
import { ApiOkResponse, ApiExtraModels, getSchemaPath } from '@nestjs/swagger';
import { ApiUserResDto, PaginatedDto } from './dto/api-res.dto';

/** 用户信息返回限定装饰器 */
export const ApiUserResponse = <TModel extends Type<any>>(model: TModel) => {
  return applyDecorators(
    ApiOkResponse({
      schema: {
        title: `UserResponseOf${model.name}`,
        allOf: [
          { $ref: getSchemaPath(ApiUserResDto) },
          {
            properties: {
              user: {
                type: 'object',
                $ref: getSchemaPath(model)
              }
            }
          }
        ]
      }
    }),
    ApiExtraModels(ApiUserResDto),
    ApiExtraModels(model)
  );
};`

apiUserResDto课程是:

export class ApiUserResDto<Dto> {
  user: Dto;
}

根据 cli 提示,只需更改ApiExtraModels(ApiUserResDto)ApiExtraModels(() => ApiUserResDto),应该可以解决此问题。

于 2021-11-26T07:18:34.597 回答