NestJS: Cannot get nested object in console.log. Error: Property x does not exist on type Dto

NestJS: Cannot get nested object in console.log. Error: Property x does not exist on type Dto

出于某种原因,我无法让嵌套对象显示在我的控制台日志中。已经尝试寻找解决方案大约一天了。

这是我的 JSON:

{
   "data": {
      "assets": [
              {
                 "asset_name": "Image1.jpg",
                 "asset_type": "photo",
                 "is_featured": true,
                 "asset_id": "alksjd78987120-kjahsdkjahsd61238-kjahsdkjahsjkdh",
                 "action": "delete"
              },
              {
                  "asset_name": "Image2.jpg",
                  "asset_type": "photo",
                  "action": "add"
              }
       ]
     }
   }

我试图在我的控制器文件中显示 console.log(data.assets); 以在将其传递给服务文件之前对其进行检查,但我一直收到错误 Property 'assets' does not exist on type 'DataDto'.

这是我的DataDto.ts

export class CreateAssetsDto {
   @IsString()
   @IsOptional()
   asset_id: string;

   @ApiProperty()
   @IsString()
   asset_name: string;

   @ApiProperty({
       enum: Action
   })
   @IsEnum(Action)
   @IsNotEmpty()
   action: Action;

   @ApiProperty()
   @IsString()
   asset_type: string;

   @ApiProperty()
   @IsBoolean()
   @IsOptional()
   is_featured: boolean;
}
export class AssetsArrayDto {
   @ValidateNested()
   @Type(() => CreateAssetsDto)
   assets: CreateAssetsDto[];
}

export class DataDto {
   @ValidateNested()
   @Type(() => AssetsArrayDto)
   data: AssetsArrayDto;
}

这是我的controller.ts

@ApiBearerAuth()
@UseGuards(AuthGuard('jwt'))
@UsePipes(new ValidationPipe({ transform: true }))
@Put(':id')
async uploadAssets(
   @Request() req,
   @Param('id') campaignId: string,
   @Body() data: DataDto) {
   try {
     console.log("Data Check");
     console.log(data);

     const { assets } = data;
     console.log(data.assets[0].asset_name);
     console.log(JSON.stringify(data, null, 2));

     const a = JSON.parse(JSON.stringify(data));

     console.log(a)
     console.log(assets)

     return {
       statusCode: HttpStatus.OK,
       message: data,
     };

   } catch (error) {
     Logger.log('Validation Error: ');
     Logger.log(error);
     throw new BadRequestException(error);
   }
 }
 }

我需要做一些我错过的事情吗?根据错误,我的数据对象似乎没有将资产作为其中的一部分读取,即使我在 DTO 中指出了它。

此外,当我这样做时 console.log(data) 这就是我得到的:

Data Check
DataDto {
 data: AssetsArrayDto { assets: [ [CreateAssetsDto], [CreateAssetsDto] ] }
}

你告诉 Typescript DataDto 有一个名为 data 的 属性,类型为 AssetsArrayDto。您已经告诉 JavaScript 从 DataDto 的实例中提取 assetsDataDto 没有 assets 属性,因此 Typescript 警告您您尝试做的事情不正确。你需要像

这样的东西
@ApiBearerAuth()
@UseGuards(AuthGuard('jwt'))
@UsePipes(new ValidationPipe({ transform: true }))
@Put(':id')
async uploadAssets(
   @Request() req,
   @Param('id') campaignId: string,
   @Body() data: DataDto) {
   try {
     console.log("Data Check");
     console.log(data);

     const { assets } = data.data;   // <-- notice the .data here
     console.log(data.assets[0].asset_name);
     console.log(JSON.stringify(data, null, 2));

     const a = JSON.parse(JSON.stringify(data));

     console.log(a)
     console.log(assets)

     return {
       statusCode: HttpStatus.OK,
       message: data,
     };

   } catch (error) {
     Logger.log('Validation Error: ');
     Logger.log(error);
     throw new BadRequestException(error);
   }
 }