Reputation: 846
i am using node,nestjs and mongoose i write a get call and get the result like
[
{
"_id": "5d4a9c0b1a6bcf14775c953c",
"material_name": "ramboo",
"material_status": 0,
"is_admin_approved": true,
"createdAt": "2019-08-07T09:38:19.237Z",
"updatedAt": "2019-08-07T09:38:19.237Z",
"id": "5d4a9c0b1a6bcf14775c953c"
}
]
the service like this (material.service.ts)
async findAll(): Promise<IDMaterial[]> {
return await this.materialModel.find({is_admin_approved: true});
}
how to remove is_admin_approved field from the result like
[
{
"_id": "5d4a9c0b1a6bcf14775c953c",
"material_name": "ramboo",
"material_status": 0,
"createdAt": "2019-08-07T09:38:19.237Z",
"updatedAt": "2019-08-07T09:38:19.237Z",
"id": "5d4a9c0b1a6bcf14775c953c"
}
]
how to solve this issue any way please help me ?
Upvotes: 1
Views: 4822
Reputation: 96
Hope this code can help you. Store the results, then iterate and delete item from each object you fetched. Then return that changed array of objects.
async findAll(): Promise<IDMaterial[]> {
const result = await this.materialModel.find({is_admin_approved: true});
result.map(elem => delete elem.is_admin_approved)
return result;
}
Upvotes: 0
Reputation: 6068
You could omit is_admin_approved
from each object in the array using lodash's omit function:
async findAll(): Promise<Partial<IDMaterial>[]> {
return this.materialModel
.find({is_admin_approved: true})
.then(data => data.map(item => _.omit(item.toObject(), 'is_admin_approved')));
}
You could also mark is_admin_approved
as @Exclude
in your IDMaterial
dto class and then cast retrieved items to this dto (plainToClass(IDMaterial, item)
).
export class IDMaterial {
// ...
@Exclude()
is_admin_approved: boolean;
// ...
}
// ...
async findAll(): Promise<Partial<IDMaterial>[]> {
return this.materialModel
.find({is_admin_approved: true})
.then(data => data.map(item => plainToClass(IDMaterial, item)));
}
Upvotes: 1