[英]How do I return an id string instead of a _bsontype with NestJS Serialization
使用時
@UseInterceptors(ClassSerializerInterceptor)
就像這里的文檔中解釋的那樣
我得到了所需的過濾結果,但是在使用mongodb時,id設置為_bsontype
格式,而不是像以前那樣沒有攔截器的普通string
:
{
"id": {
"_bsontype": "ObjectID",
"id": {
"0": 92,
"1": 108,
"2": 182,
"3": 85,
"4": 185,
"5": 20,
"6": 221,
"7": 12,
"8": 56,
"9": 66,
"10": 131,
"11": 172
}
},
"createdAt": "2019-02-20T02:07:17.895Z",
"updatedAt": "2019-02-20T02:07:17.895Z",
"firstName": "The First Name",
"lastName": "The Last Name",
"email": "giberish@gmail.com"
}
我如何將其轉換回這樣的普通id字符串?
{
"id": "5c6cb655b914dd0c384283ac",
"createdAt": "2019-02-20T02:07:17.895Z",
"updatedAt": "2019-02-20T02:07:17.895Z",
"firstName": "The First Name",
"lastName": "The Last Name",
"email": "giberish@gmail.com"
"password": "okthen"
}
您可以將class-transformer的@Transform()
與toPlainOnly
選項toPlainOnly
:
import { Transform } from 'class-transformer';
@Entity()
export class User {
@ObjectIdColumn()
@Transform((value) => value.toString(), { toPlainOnly: true })
_id: ObjectID;
ClassSerializerInterceptor
內部使用類轉換器的classToPlain()
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.