User Profile Update fail - TypeError: user.then is not a function
haganbt opened this issue · 1 comments
haganbt commented
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 commented
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."
});
}
});
};