[英]Nestjs ClassSerializerInterceptor doesn't display _id
I have an issue properly exposing the _id using the Serializer.我在使用序列化程序正确公开 _id 时遇到问题。
I use:我用:
@UseInterceptors(ClassSerializerInterceptor)
@SerializeOptions({ strategy: 'excludeAll' })
The defined Class:定义的 Class:
export class UpdatedCounts {
@Expose()
_id: ObjectId;
@Expose()
aCount: number;
@Expose()
bCount: number;
constructor(partial: Partial<MyDocument>) {
Object.assign(this, partial);
}
}
The object in console.log() before it runs through the Serializer console.log() 中的 object 在通过序列化程序之前运行
{
_id: new ObjectId("61c2256ee0385774cc85a963"),
bannerImage: 'placeholder2',
previewImage: 'placeholder',
aCount: 1,
bCount: 0,
}
The object being returned:返回的 object:
{
"_id": {},
"aCount": 1,
"bCount": 0
}
So what happened to my _id?那么我的_id怎么了?
I tried using string type instead of ObjectId but that also does not work我尝试使用字符串类型而不是 ObjectId 但这也不起作用
I do not want to use @Exclude since there are 10 more props which I left out in the example console.log(), and it should be easier to exclude all and just use these 3我不想使用@Exclude,因为我在示例 console.log() 中遗漏了另外 10 个道具,排除所有道具并只使用这 3 个应该更容易
Just use @Transform
:只需使用@Transform
:
@Expose()
@Transform((params) => params.obj._id.toString())
_id: ObjectId;
You can not just send ObjectId with JSON.您不能只使用 JSON 发送 ObjectId。 You must convert it to a string.您必须将其转换为字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.