germainelol
germainelol

Reputation: 3331

Jade - Accessing a variable passed to it

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,
});

Upvotes: 0

Views: 261

Answers (1)

monkeyinsight
monkeyinsight

Reputation: 4859

- if (typeof session.user != 'undefined' && session.user.role === 'default')

Upvotes: 3

Related Questions