【发布时间】:2019-12-14 14:43:35
【问题描述】:
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) 显示如下
{ language: 'en',
loggedIn: true,
warningMessage: 'verify',
username: 'bill',
email: 'bill@gmail.com', // <===== Why is this still here?
fullName: 'Bill',
location: '/contact',
gender: 'male',
avatarId: '338fcdd84627317fa66aa6738346232781fd3c4b.jpg',
country: 'US' }
如果我愿意
console.log(account.shared.email); // undefined
我收到undefined,但如果我在前端使用 console.log(response),电子邮件仍然在对象中
【问题讨论】:
-
我认为这是你无法删除它的原因。stackoverflow.com/questions/23342558/…
-
因为mongoose的实现,你也可以在mozzila docdeveloper.mozilla.org/en-US/docs/Web/JavaScript/Reference/…中查看delete用法