简体   繁体   English

玉-访问传递给它的变量

[英]Jade - Accessing a variable passed to it

So I have a user stored in session when they log in on my Node.js application. 因此,我有一个用户在登录Node.js应用程序时存储在session中。 I have admins and default users, if I output #{session.user.role} in the Jade template, I get default or admin appear. 我有管理员和默认用户,如果我在Jade模板中输出#{session.user.role} ,则会显示defaultadmin 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? 而且,当在我的页面上进行测试时,如果没有人登录,我可以使用-if(session.user)隐藏页面上的项目,但是如何为非管理员用户隐藏项目?

I have tried putting in -if(session.user.role === 'default') but I get an error saying Cannot read property 'role' of undefined . 我曾尝试放入-if(session.user.role === 'default')但收到一条错误消息,提示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: 这是使用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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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