Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Profile Update fail - TypeError: user.then is not a function #194

Open
haganbt opened this issue Feb 24, 2017 · 1 comment
Open

User Profile Update fail - TypeError: user.then is not a function #194

haganbt opened this issue Feb 24, 2017 · 1 comment

Comments

@haganbt
Copy link

haganbt commented Feb 24, 2017

Any profile updates (press update profile button) fails with the below exception. This is true for "Profile Information" and "Change Password".

Stack: Node, Express, Bootstrap, no css or js frameworks, Mocha, MySQL

Node: v6.3.1

TypeError: user.then is not a function
    at exports.accountPut (/Users/ben/WebstormProjects/roadmeer/controllers/user.js:154:8)
@haganbt
Copy link
Author

haganbt commented Feb 25, 2017

Not too sure why we cannot call user.then as user appears return a promise. Interested to hear if anyone can advise.

As a workaround, I have simplified the control flow.

/**
 * PUT /account
 * Update profile information OR change password.
 */
exports.accountPut = function(req, res, next) {
  if ("password" in req.body) {
    req
      .assert("password", "Password must be at least 4 characters long")
      .len(4);
    req.assert("confirm", "Passwords must match").equals(req.body.password);
  } else {
    req.assert("email", "Email is not valid").isEmail();
    req.assert("email", "Email cannot be blank").notEmpty();
    req.sanitize("email").normalizeEmail({ remove_dots: false });
  }

  const errors = req.validationErrors();

  if (errors) {
    req.flash("error", errors);
    return res.redirect("/account");
  }

  let d = {
    email: req.body.email,
    name: req.body.name,
    gender: req.body.gender,
    location: req.body.location,
    website: req.body.website
  };

  if ("password" in req.body) {
    d = { password: req.body.password };
  }

  const user = new User({ id: req.user.id });

  user.save(d, { patch: true })
    .then(() => {
      if ("password" in req.body) {
        req.flash("success", { msg: "Your password has been changed." });
      } else {
        req.flash("success", {
          msg: "Your profile information has been updated."
        });
      }
      res.redirect("/account");
    })
    .catch(err => {
      if (err.code === "ER_DUP_ENTRY") {
        req.flash("error", {
          msg: "The email address you have entered is already associated with another account."
        });
      }
    });
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant