Reputation: 5150
static profile = async (req: Request, res: Response) => {
try {
const { username } = req.params;
const account = await userModel
.findOne({ 'shared.username': username })
.exec();
if (account) {
delete account.shared.email; // <======= Why isnt this deleteing email
console.log(account.shared);
res
.status(200)
.send({ shared: account.shared, lastSeen: account.updatedAt });
} else res.status(400).send({ message: 'Server Error' });
} catch (error) {
res.status(400).send({ message: 'Server Error', error });
}
};
console.log(account.shared) shows the below
{ language: 'en',
loggedIn: true,
warningMessage: 'verify',
username: 'bill',
email: '[email protected]', // <===== Why is this still here?
fullName: 'Bill',
location: '/contact',
gender: 'male',
avatarId: '338fcdd84627317fa66aa6738346232781fd3c4b.jpg',
country: 'US' }
if id do
console.log(account.shared.email); // undefined
I get undefined
but if I console.log(response) on the front end the email is still in the object
Upvotes: 0
Views: 56
Reputation: 957
You declared const account
, might be that declaring account as a constant prevents the delete
to alter the object. Try using var account
instead.
Upvotes: 0
Reputation: 4404
You can omit it using ES6 syntax like this
if (account) {
const {email, ...otherFields} = account.shared;
console.log(otherFields);
res
.status(200)
.send({ shared: otherFields, lastSeen: account.updatedAt });
} else res.status(400).send({ message: 'Server Error' });
}
Upvotes: 1