So I have a user stored in session when they log in on my Node.js application. I have admins and default users, if I output #{session.user.role} in the Jade template, I get default or admin appear. And when testing on my page I can hide items on the page using -if(session.user) to hide an item if no one is logged in, but how do I hide items for users which aren't admins?
I have tried putting in -if(session.user.role === 'default') but I get an error saying Cannot read property 'role' of undefined. So that won't work, any ideas on how to do this anyone?
Here is the User Schema using Mongoose:
var User = new Schema({ 'key' : { unique : true, type : Number, default: getId }, 'username' : { type : String, validate : [validatePresenceOf, 'a Username is required'], set : toLower, index : { unique : true } }, 'password' : String, 'role' : String, });
session.user.roleoutputs default or admin, so it must exist. You sure the undefined-error doesn't just occur for users that are not logged in? If so you could writeif(session.user && session.user.role === 'default').