Answer the question
In order to leave comments, you need to log in
How to properly serialize in nestjs?
I am getting data from mongi via nestjs . Let's say I want to get all users Here is the control that processes the endpoint
@Get('/getAll')
async getAll() {
return this.userService.getAll();
}
async getAll(): Promise<User[]> {
try {
return await this.userModel.find().exec();
} catch (error) {
throw new BadRequestException('Users not found');
}
}
{
"lastname": "Gabbana",
"_id": "5f782ace2cdfcd297c373ba6",
"name": "Dolce",
"password": "$2b$10$DSomsGAk4FuXUH6Knnmdz.9L2DWlFTfH.VOIM8VKYlDroOwKeh7kW",
"__v": 0
}
@Schema()
export class User extends Document {
@Prop()
name: string;
@Prop({ default: '' })
lastname: string;
@Exclude()
@Prop()
password: string;
}
export const UserSchema = SchemaFactory.createForClass(User);
{
"$__": {
"strictMode": true,
"selected": {},
"getters": {},
"_id": {
"_bsontype": "ObjectID",
"id": {
"type": "Buffer",
"data": [
95,
120,
42,
206,
44,
223,
205,
41,
124,
55,
59,
166
]
}
},
"wasPopulated": false,
"activePaths": {
"paths": {
"role": "init",
"registerAt": "init",
"status": "init",
"lastname": "init",
"_id": "init",
"name": "init",
"email": "init",
"phone": "init",
"password": "init",
"__v": "init"
},
"states": {
"ignore": {},
"default": {},
"init": {
"_id": true,
"role": true,
"registerAt": true,
"status": true,
"lastname": true,
"name": true,
"email": true,
"phone": true,
"password": true,
"__v": true
},
"modify": {},
"require": {}
},
"stateNames": [
"require",
"modify",
"init",
"default",
"ignore"
]
},
"pathsToScopes": {},
"cachedRequired": {},
"session": null,
"$setCalled": [],
"emitter": {
"_events": {},
"_eventsCount": 0,
"_maxListeners": 0
},
"$options": {
"skipId": true,
"isNew": false,
"willInit": true,
"defaults": true
}
},
"isNew": false,
"$locals": {},
"$op": null,
"_doc": {
"lastname": "Gabbana",
"_id": {
"_bsontype": "ObjectID",
"id": {
"type": "Buffer",
"data": [
95,
120,
42,
206,
44,
223,
205,
41,
124,
55,
59,
166
]
}
},
"name": "Dolce",
"password": "$2b$10$DSomsGAk4FuXUH6Knnmdz.9L2DWlFTfH.VOIM8VKYlDroOwKeh7kW",
"__v": 0
},
"$init": true
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question