【问题标题】:ES6 combine two variables into an existing javaScript objectES6 将两个变量组合成一个现有的 javaScript 对象
【发布时间】:2019-12-14 16:23:02
【问题描述】:

我目前在数据对象之外返回createdAtupdatedAt,我想将它们添加到数据对象中,以便我可以返回单个对象。

我还注意到我从某个地方收到了$init: true,如何摆脱它?

  static profile = async (req: Request, res: Response) => {
    try {
      const { username } = req.params;
      const account = await userModel
        .findOne({ 'shared.username': username })
        .exec();
      if (account) {
        const {email, loggedIn, location, warningMessage, ...data} = account.shared;
        const { updatedAt, createdAt } = account;
        res
          .status(200)
          .send({ data, updatedAt, createdAt }); // <=== How to combine updatedAt & createdAt into data?
      } else res.status(400).send({ message: 'Server Error' });
    } catch (error) {
      res.status(400).send({ message: 'Server Error', error });
    }
  };

当前结果是

createdAt: "2019-12-13T12:15:05.031Z"
data: {
 $init: true // <=== why is this here, where did it come from?
 avatarId: "338fcdd84627317fa66aa6738346232781fd3c4b.jpg"
 country: "AS"
 fullName: "Bill"
 gender: "male"
 language: "en"
 username: "bill"
}
updatedAt: "2019-12-14T16:07:34.923Z"

【问题讨论】:

  • 与其使用...data 休息模式,不如明确说明您想要在最终对象中的哪些属性,例如send({ updatedAt: data.updatedAt, country: data.shared.country, … })
  • 你的意思是.send({data: {...data, updatedAt, createdAt }})
  • 关于$init: true 我猜account 是从mongo db 中检索到的。 IIRC,您不应该直接使用结果,但您需要使用 toObject 将其转换为普通对象。
  • const acc = account.toObject(); const { email, loggedIn, location, ...data } = acc.shared; const { updatedAt, createdAt } = acc;
  • @Bill 我猜它应该是这样的。上次用mongodb已经很久了。

标签: javascript node.js ecmascript-6


【解决方案1】:

使用扩展运算符:

{ ...data, updatedAt, createdAt }

请注意,您的变量名将成为您的索引

【讨论】:

    【解决方案2】:

    删除$init,仅此而已。

    const {$init, ...cleanData} = data;
    

    使用createdAtupdatedAt 创建数据对象

    const finalData = {...cleanData, createdAt, updatedAt}
    

    编码愉快!

    【讨论】:

    • 确实可以通过这种方式摆脱$init。但如果它一开始就不应该存在,那么摆脱它的正确方法是弄清楚它在哪里/为什么被添加,并确保它没有被添加。
    • 已经有 cmets 解决如何将 Mongoose 文档转换为 POJO。我认为为其他涉足休息/传播的人添加有用的信息会更好。一遍又一遍。
    【解决方案3】:

    像这样使用 .toObject() :

    static profile = async (req: Request, res: Response) => {
    try {
      const { username } = req.params;
      const _account = await userModel
        .findOne({ 'shared.username': username })
        .exec();
      const account = _account.toObject()
      if (account) {
        const {email, loggedIn, location, warningMessage, ...data} = account.shared;
        const { updatedAt, createdAt } = account;
        res
          .status(200)
          .send({ data: { ...data, ...{ updatedAt, createdAt }} });
      } else res.status(400).send({ message: 'Server Error' });
    } catch (error) {
      res.status(400).send({ message: 'Server Error', error });
    }
    

    };

    【讨论】:

      猜你喜欢
      • 2017-02-04
      • 1970-01-01
      • 2016-11-12
      • 2021-07-07
      • 2018-05-13
      • 2018-11-23
      • 2022-01-04
      • 2012-03-04
      • 2021-11-08
      相关资源
      最近更新 更多