用户 waitOn
数据订阅:
waitOn: function(){
if(Meteor.user()){
var current_user_admin_status = Meteor.user().admin;
console.log(current_user_admin_status);
return Meteor.subscribe('users', current_user_admin_status);
}
}
但是 current_user_admin_status
总是返回 null
,即使我可以进入数据库并找到 admin
字段
db.users.find({"_id": "6Mqx5Ky92bZfhaX8A"}, {"admin": 1})
{ "_id" : "6Mqx5Ky92bZfhaX8A", "admin" : true }
我在客户端定义这个变量,如果当前用户是管理员,传递给服务器只发布用户集合,但它总是被 null
if else
声明
Meteor.publish('users', function(current_user_admin_status){
if(!this.userId){
console.log('you are not signed in');
}
else if( current_user_admin_status = 'null' ){
console.log('you are not an admin');
} else if ( current_user_admin_status = 'false' ) {
console.log('you are not an admin');
} else if (current_user_admin_status = 'undefined'){
console.log('you are not an admi');
} else {
console.log('you are logged in as an admin');
return Meteor.users.find({}, {fields: {createdAt: 1, admin: 1, emails: 1, username: 1, first_name: 1, last_name: 1}});
}
});
最佳答案
这里有几个要点需要注意。首先,关于问题评论,将管理字段添加到 user.profile
是不安全的,除非您添加了特定的 allow/deny
防止它被编辑的规则。请参阅 this article 中的第一点.默认情况下,user.profile
字段可由其用户编辑,通常您应该在其外部存储敏感字段,例如 admin。
其次,您没有在客户端收到 user.profile.admin
的最可能原因是您没有发布它。您应该首先为登录用户创建一个发布,以将注释中注明的默认字段之外的字段发送给用户。
最后,有一个用于处理授权的优秀且广泛使用的包 roles
,它已经解决了上述安全问题。我建议使用:https://github.com/alanning/meteor-roles
关于javascript - meteor 用户属性未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29975316/