I have a function in my app.js that allows the user-id to be accessible in pug templates.
app.use(function (req, res, next) {
res.locals.currentUser = req.session.userId;
next();
});
When logged in, I can access the id. However, when not logged in, the conditional statement in my pug template does not work as expected. Here is my code in the pug template:
if {currentUser}
//do something
else
//do something else.
The else
part does not execute. I have checked and found that the variable currentUser
is an object
. Using JSON.stringify({currentUser})
from the pug template outputs {}
.
When logged in, currentUser
returns a string id, and when logged out, it returns an empty object. Both conditions satisfy the if{currentUser}
clause.
Can anyone offer some help with this issue?