在 METEORJS 应用程序的客户端,我有一个显示一些用户的 Controller 。
我的 Meteor.user() 函数有问题,错误是:Meteor.user(...) 未定义。
这是我的代码:
this.AdminUsersController = RouteController.extend({
template: "Admin",
yieldTemplates: {
'AdminUsers': { to: 'AdminSubcontent'}
},
onBeforeAction: function() {
var permissions = Meteor.user().profile.permissions;
if (permissions && permissions.indexOf('Users') != -1)
this.next();
else this.redirect('/admin/dashboard');
},
action: function() {
if(this.isReady()) { this.render(); } else { this.render("Admin"); this.render("loading", { to: "AdminSubcontent" });}
/*ACTION_FUNCTION*/
},
isReady: function() {
var subs = [
Meteor.subscribe("users")
];
var ready = true;
_.each(subs, function(sub) {
if(!sub.ready())
ready = false;
});
return ready;
},
data: function() {
var data = {
params: this.params || {},
users: Users.find({labo_id: Meteor.user().profile.labo_id}, {sort: {createdAt:-1}})
};
return data;
},
onAfterAction: function() {
}});
它在数据函数中。 我尝试检索连接到已登录用户并获得相同 labo_id 字段的所有用户...
我不知道为什么它给我这个,因为在 onBeforeAction 函数中,我可以访问 Meteor.user(),特别是他的个人资料...
有人知道我该怎么做才能让它运行吗?
感谢您以后的回答:)
最佳答案
这是一个时间问题。 Meteor.user() 不一定返回数据,如果它还没有被加载。然而,Meteor.userId() 将返回用户记录的 _id(如果他们已登录)。如果您可以更改查询以依赖该 _id,那么它就可以工作。
根据您使用的路由器,您可以添加一个 resolve: 条目以确保该路由在激活之前等待用户记录加载,然后您的查询才能工作。
关于javascript - Meteor.user() 在客户端 Controller 中未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39751108/