0

My Routes:

router.use(function(req, res, next) { res.locals.currentUser = req.user; next(); }); /* GET home page. */ router.get('/', function(req, res, next) { console.log(res.locals.currentUser.username); ==>> this is getting printed in console. res.render('index'); }); 

My index.handelbars

{{currentUser}} ===> this is getting displayed {{currentUser.username}} ===> this is not 

My User Schema

const UserSchema = new Schema({ fullName: String, username: { type: String, required: true }, password: { type: String, required: true }, isMember: { type: Boolean, default: false }, isAdmin: { type: Boolean, default: false } }); 

I am trying to access one of 'currentUser' object's property. Although the object itself gets displayed in the template but its property doesn't.

1 Answer 1

1

Change this

res.locals.currentUser = req.user;

to

res.locals.currentUser = req.user.lean(); // add lean() 
Sign up to request clarification or add additional context in comments.

1 Comment

could also use toObject() method. Apparently, the latest version of HandleBars wouldn't let you use prototype methods or properties of an object.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.