I am getting different ID value in JSON response when checking variable as id & _id in DTO
CASE I ( using id as variable )
DTO for the response
export class UserDetail {
@Expose( )
id: string; //HERE ID is used which gives original id as in db
@Expose( )
name: string;
@IsOptional( )
@Expose( )
mobile: string;
}
Response for this is:
{
"id": "6229df5cc32d3aaef32525e0", // Correct id as in db
"name": "Name1 Title1",
"mobile": "AB391C339",
},
{
"id": "6229df7bc32d3aaef32525e3", // Correct id as in db
"name": "Name2 Title2",
"mobile": "CDE393F339",
}
CASE II ( using _id as variable )
Now changing DTO
export class UserDetail {
@Expose( )
_id: string; //HERE _id is used which gives different id value from original one and subsequent ids are in incremental fashion
@Expose( )
name: string;
@IsOptional( )
@Expose( )
mobile: string;
}
Response for this is:
{
"_id": "624023c8193f2404f8312ccb", // Not same as present in db
"name": "Name1 Title1",
"mobile": "AB391C339"
},
{
"_id": "624023c8193f2404f8312ccc", // Not same as present in db
"name": "Name2 Title2",
"mobile": "CDE393F339"
},
Schema for user is:
@Schema( )
export class User extends Document {
@Prop( { required: true } )
name: string;
@Prop( { required: true } )
mobile: string;
}
Another Observation: With the use of _id in dto, all the ids in response are in incremental fashion. eg. cb, cc, cd, ce
& with only id in dto it's showing ids as original id in db.
Library & Framework Used: NestJs, Fastify, Mongoose, Class-Transformer