简体   繁体   English

NestJS:无法在 console.log 中获得嵌套的 object。 错误:类型 Dto 上不存在属性 x

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

for some reason I cannot get the nested object to display in my console log.由于某种原因,我无法在控制台日志中显示嵌套的 object。 Been trying to look for a solution for about a day now.一直在尝试寻找解决方案大约一天。

Here is my JSON:这是我的 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"
              }
       ]
     }
   }

I am trying to display console.log(data.assets);我正在尝试显示console.log(data.assets); in my controller file to check it before passing it to the service file but I keep on getting the error Property 'assets' does not exist on type 'DataDto'.在我的 controller 文件中,在将其传递给服务文件之前对其进行检查,但我不断收到错误Property 'assets' does not exist on type 'DataDto'.

Here is my DataDto.ts这是我的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;
}

Here is my controller.ts这是我的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);
   }
 }
 }

Do I need to do something that I missed?我需要做一些我错过的事情吗? Based on the error it seems like my data object is not reading assets as part of it even though I indicated it in the DTO.根据错误,我的数据 object 似乎没有将资产作为其中的一部分读取,即使我在 DTO 中指出了它。

Also, when I do console.log(data) this is what I get:另外,当我执行console.log(data)这就是我得到的:

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

You've told Typescript that DataDto has a property called data that is of type AssetsArrayDto .您已经告诉 Typescript DataDto有一个名为data的属性,其类型为AssetsArrayDto You've told JavaScript to pull assets off of an instance of DataDto .您已经告诉 JavaScript 从DataDto实例中提取assets DataDto does not have an assets property, so Typescript is warning you that what you're trying to do is incorrect. DataDto没有assets属性,因此 Typescript 警告您您尝试做的事情不正确。 You'd need something like你需要类似的东西

@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);
   }
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿错误:类型{…}上不存在属性&#39;log&#39;-Console.log() - typescript error: Property 'log' does not exist on type {…} - Console.log() 错误TS2339:类型&#39;{new():控制台上不存在属性&#39;log&#39;; 原型:控制台; }” - error TS2339: Property 'log' does not exist on type '{ new (): Console; prototype: Console; }' “缓存”类型不存在属性“get”。 NestJs项目 - Property 'get' does not exist on type 'Cache'. NestJs project console.log 中没有 output。 初学者问题 - No output in console.log. Beginner Question 如何在 TypeScript 中使用嵌套对象? 类型“对象”上不存在属性 - How to use nested objects in TypeScript? Property does not exist on type 'object' 类型 object 上不存在属性 - Property does not exist on type object 我正在尝试向我的反应组件发出 axiosGET 请求,我在 console.log 上获取对象。 但是当我尝试渲染它时,我得到一个“未定义” - i'm trying to make a axiosGET request to my react component, i get the object on the console.log. But when i try to render it i get a "is not defined" 类型上不存在属性“_id”。 尝试访问 Nestjs 应用程序中 promise 结果的属性 _id 时出现类型错误 - Property '_id' does not exist on type. Getting type error when trying to access property _id on result of a promise in nestjs application 属性“”在类型“对象”上不存在 - Property '' does not exist on type 'Object' 当属性存在时,类型错误时属性不存在 - Property does not exist on type error when the property does exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM