Skip to content

Commit

Permalink
trying to fix an error
Browse files Browse the repository at this point in the history
  • Loading branch information
abutler911 committed Oct 27, 2023
1 parent ed77a20 commit 6de4147
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
8 changes: 8 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ app.get("/test-flash", (req, res) => {
res.redirect("/login");
});

function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
req.flash("error_msg", "Please log in to view this resource");
res.redirect("/login");
}

// Error handling
app.use((err, req, res, next) => {
console.error(`[Error] ${err.stack}`);
Expand Down
17 changes: 4 additions & 13 deletions routes/affirmationRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,19 @@ router.get("/affirmation-setup", async (req, res) => {

if (!user || !user.affirmations) {
const userId = req.session.passport.user;
user = await User.findById(userId);
user = user ? JSON.parse(JSON.stringify(user)) : null; // Deep clone to plain object
user = await User.findById(userId).select(
"username affirmationDuration affirmations"
); // Explicitly select fields
user = user ? user.toObject() : null; // Convert to plain object
}

// Debugging Step 1: Check the Conversion
console.log(
"Is Plain Object?",
user instanceof Object &&
!(user instanceof Array) &&
!(user instanceof Function)
);

const categoriesWithSpaces = {};
if (user && user.affirmations) {
for (const [key, value] of Object.entries(user.affirmations)) {
categoriesWithSpaces[camelCaseToSpaces(key)] = value;
}
}

// Debugging Step 2: Check the Data
console.log("Categories with Spaces:", categoriesWithSpaces);

res.render("affirmation-setup", { user, categoriesWithSpaces });
});

Expand Down

0 comments on commit 6de4147

Please sign in to comment.