I have an API call to get all the users of my application. I have a problem, that the getAll method returns each users's password encrypted. I don't want to return the password.
That's my method:
async getAll(pagination) {
try {
const { UserAdmin: userAdminSchema } = this.getSchemas();
let usersAdmins = await userAdminSchema.paginate({}, {
sort: { created: 'desc' },
limit: pagination.limit ? parseInt(pagination.limit) : 10,
page: pagination.page ? parseInt(pagination.page) + 1 : 1,
});
if (!usersAdmins) {
throw new errors.NotFound('No UserAdmins found.');
}
usersAdmins.docs.map(async (u) => {
delete u.password;
});
return usersAdmins
} catch (error) {
throw error;
}
{
"docs": [
{
"roles": [
"Admin",
"Read",
"Modify",
],
"_id": "5ffecc12a687a30580239a10",
"name": "john",
"email": "[email protected]",
"password": "$2a$10$K6oKiiWKsIWmAGgkG7TmH.WjVpraNg45Uc0nber3FnF26oEzdgTS2",
"created": "2021-01-13T10:31:46.982Z",
"__v": 0
},
{
"roles": [
"Read"
],
"_id": "5ffea1f1dd60b80718af6e3b",
"name": "Megan",
"email": "[email protected]",
"password": "$2a$10$V8ARIjxhEHCC1COcvtQEmOZ3IfXm5oiANdLmyHyipy1GmWSD3ctWK",
"created": "2021-01-13T07:32:01.665Z",
"__v": 0
},
],
"totalDocs": 2,
"limit": 10,
"totalPages": 1,
"page": 1,
"pagingCounter": 1,
"hasPrevPage": false,
"hasNextPage": false,
"prevPage": 1,
"nextPage": null
}
I just want to the delete the password. How I can achieve this? The map I have done, still returns the password:
usersAdmins.docs.map(async (u) => {
delete u.password;
});