[英]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.