【问题标题】:Using variables to build dynamic MongoDB/Mongoose query of nested object使用变量构建嵌套对象的动态 MongoDB/Mongoose 查询
【发布时间】:2018-07-14 21:17:40
【问题描述】:

我的应用有 4 个相同的 Node/Express API 路由,用于删除 MongoDB 数据库中的嵌套对象属性。这 4 条路由在语法上的唯一区别是字符串值(“facebook”、“google”、“twitter”或“github”。这是四种路由之一:

  app.get("/unlink/facebook", async (req, res) => {
    await User.update(
      { _id: req.user._id },
      {
        $unset: {
          "authProviders.facebook.facebookId": "",
          "authProviders.facebook.facebookDisplayName": "",
          "authProviders.facebook.facebookEmail": ""
        }
      }
    );
    res.redirect("/preferences");
  });

我的目标是通过在 Express 路由的 URL 上添加一个参数,将这四个路由重构为一个端点,该参数将成为代表四种社交媒体帐户类型之一的字符串变量。路由的重点是动态确定要在 MongoDB UserauthProviders 对象中 $unset(删除)哪个社交媒体帐户属性文件。

我尝试使用 ES6 模板文字构建 MongoDB 查询以访问必要的对象属性,但是我收到错误:“SyntaxError: Unexpected template string”

以下是我尝试使用 ES6 模板文字和社交媒体变量重构为单一端点的代码:

app.get("/unlink/:account", async (req, res) => {
      let accountType = req.params.account;
      let email = accountType + "Email";
      let id = accountType + "id";
      let displayName = accountType + "DisplayName";
    await User.update(
      { _id: req.user._id },
      {
        $unset: {
          `authProviders[${accountType}][${id}]`: "",
          `authProviders[${accountType}][${email}]`: "",
          `authProviders[${accountType}][${displayName}]` : ""
        }
      }
    );
    res.redirect("/preferences");
  });

这是 MongoDB 文档:

关于如何使这项工作的任何想法?我似乎无法弄清楚如何构造 MongoDB 查询以使用变量访问对象属性。

【问题讨论】:

    标签: node.js mongodb mongoose ecmascript-6 template-literals


    【解决方案1】:

    好的,我想出了如何做到这一点。发布解决方案以防万一其他新生开发人员遇到此问题。

    app.get("/unlink/:account", async (req, res) => {
        let accountType = req.params.account,
            query1 = "authProviders." + accountType + "." + accountType + "Id",
            query2 = "authProviders." + accountType + "." + accountType + "Email",
            query3 = "authProviders." + accountType + "." + accountType + "DisplayName";
        await User.update(
          { _id: req.user._id },
          {
            $unset: {
              [query1]: "",
              [query2]: "",
              [query3]: ""
            }
          }
        );
        res.redirect("/preferences");
      });
    

    【讨论】:

      【解决方案2】:

      你可以这样做:

      app.get("/unlink/:account", async (req, res) => {
            let accountType = req.params.account;
          await User.update(
            { _id: req.user._id },
            {
              $unset: {
                authProviders[`${accountType}.${accountType}id`]: "",
                authProviders[`${accountType}.${accountType}email`]: "",
                authProviders[`${accountType}.${accountType}displayName`] : ""
              }
            }
          );
          res.redirect("/preferences");
        });
      

      【讨论】:

      • 似乎 $unset 对象属性应该是字符串(用引号引起来)。使用您提供的代码会导致 Node.js 中出现“SyntaxError: Unexpected token [”错误。这就是我开始尝试使用模板文字的最初原因。
      猜你喜欢
      • 2018-11-10
      • 2013-06-03
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      • 2021-11-12
      • 2021-01-24
      • 1970-01-01
      • 2017-06-26
      相关资源
      最近更新 更多