/**
* POST /account/profile
* Update profile information.
*/
export const postUpdateProfile = async (req: Request, res: Response, next: NextFunction) => {
await check("email", "Please enter a valid email address.").isEmail().run(req);
// eslint-disable-next-line @typescript-eslint/camelcase
await sanitize("email").normalizeEmail({ gmail_remove_dots: false }).run(req);
const errors = validationResult(req);
if (!errors.isEmpty()) {
req.flash("errors", errors.array());
return res.redirect("/account");
}
const user = req.user as LandlordDocument;
Landlord.findById(user.id, (err, user: LandlordDocument) => {
if (err) { return next(err); }
user.email = req.body.email || "";
user.profile.name = req.body.name || "";
user.profile.gender = req.body.gender || "";
user.profile.location = req.body.location || "";
user.profile.website = req.body.website || "";
user.save((err: WriteError) => {
if (err) {
if (err.code === 11000) {
req.flash("errors", { msg: "The email address you have entered is already associated with an account." });
return res.redirect("/account");
}
return next(err);
}
req.flash("success", { msg: "Profile information has been updated." });
res.redirect("/account");
});
});
};
Doesn't user in "const user = req.user as LandlordDocument;" already contain everything that the user in the database contains (which you are looking up via " Landlord.findById(user.id"). Basically aren't you looking up another user with the exact same values as req.user and then shadowing the user variable? Can't you simplify it to this:
/**
* POST /account/profile
* Update profile information.
*/
export const postUpdateProfile = async (req: Request, res: Response, next: NextFunction) => {
await check("email", "Please enter a valid email address.").isEmail().run(req);
// eslint-disable-next-line @typescript-eslint/camelcase
await sanitize("email").normalizeEmail({ gmail_remove_dots: false }).run(req);
const errors = validationResult(req);
if (!errors.isEmpty()) {
req.flash("errors", errors.array());
return res.redirect("/account");
}
const user = req.user as LandlordDocument;
if (err) { return next(err); }
user.email = req.body.email || "";
user.profile.name = req.body.name || "";
user.profile.gender = req.body.gender || "";
user.profile.location = req.body.location || "";
user.profile.website = req.body.website || "";
user.save((err: WriteError) => {
if (err) {
if (err.code === 11000) {
req.flash("errors", { msg: "The email address you have entered is already associated with an account." });
return res.redirect("/account");
}
return next(err);
}
req.flash("success", { msg: "Profile information has been updated." });
res.redirect("/account");
});
};
That way you don't have an extra database lookup and an extra "user" variable.
Referring to this code in users.ts:
Doesn't user in "const user = req.user as LandlordDocument;" already contain everything that the user in the database contains (which you are looking up via " Landlord.findById(user.id"). Basically aren't you looking up another user with the exact same values as req.user and then shadowing the user variable? Can't you simplify it to this:
That way you don't have an extra database lookup and an extra "user" variable.