简体   繁体   中英

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,
});
- if (typeof session.user != 'undefined' && session.user.role === 'default')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM