skip to Main Content

My app has 4 identical Node/Express API routes that are meant to remove an nested object property within a MongoDB database. The only difference in syntax between these 4 routes is a string value (either “facebook”, “google”, “twitter” or “github”. Here is one of the four routes:

  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");
  });

My aim is to refactor these four routes into a single endpoint by adding a parameter onto the Express route’s URL that would become a string variable representing one of the four social media account types. The point of the route would be to dynamically determine which social media account property to $unset (delete) within the authProviders object in the MongoDB User document.

I attempted to build the MongoDB query to access the necessary object property using ES6 template literals, however I receive an error: “SyntaxError: Unexpected template string”.

Below is the code for my attempt at refactoring to a singular endpoint using ES6 template literals and the social media variable:

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");
  });

Here is the MongoDB document:

enter image description here

Any ideas on how to make this work?? I can’t seem to figure out how to structure the MongoDB query to access the object property using variables.

2

Answers


  1. Chosen as BEST ANSWER

    OK, I figured out how to make this happen. Posting resolution just in case another nascent developer runs into this.

    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. You could do it like this :

    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");
      });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search