为什么不能使用`delete`删除对象中的项目

问题描述 投票:0回答:1
  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: '[email protected]',   // <===== Why is this still here?
  fullName: 'Bill',
  location: '/contact',
  gender: 'male',
  avatarId: '338fcdd84627317fa66aa6738346232781fd3c4b.jpg',
  country: 'US' }

如果有ID,则>]

console.log(account.shared.email); // undefined

我得到undefined,但是如果我在前端使用console.log(response),电子邮件仍然在对象中

静态配置文件=异步(要求:请求,要求:响应)=> {尝试{const {用户名} = req.params; const account = await userModel .findOne({'shared.username':username})...

node.js express mongoose
1个回答
1
投票

您可以使用ES6语法将其省略


0
投票

您声明为const account,可能是因为将帐户声明为常量会阻止delete更改对象。尝试改用var account

© www.soinside.com 2019 - 2024. All rights reserved.