简体   繁体   English

客户端控制器中未定义Meteor.user()

[英]Meteor.user() is undefined in client-side controller

In the client-side of a METEORJS application, i have a controller that display some users. 在METEORJS应用程序的客户端,我有一个显示一些用户的控制器。

I have a problem with Meteor.user() function, the error is : Meteor.user(...) is undefined. 我的Meteor.user()函数有问题,错误是:Meteor.user(...)未定义。

Here is my code : 这是我的代码:

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() {

}});

It's in the data function. 它在数据功能中。 I try to retrieve all users that are connected to the logged in user and got the same labo_id field... 我尝试检索连接到登录用户的所有用户并获得相同的labo_id字段...

I don't know why it give me that, because in the onBeforeAction function, i can access to Meteor.user(), and specially his profile... 我不知道为什么它会给我这个,因为在onBeforeAction函数中,我可以访问Meteor.user(),特别是他的个人资料......

Someone know what can i do to make it run ? 有人知道我该怎么做才能让它运行?

Thanks for your future answers :) 谢谢你的未来答案:)

This is a timing issue. 这是一个时间问题。 Meteor.user() does not necessarily return data, if it hasn't been loaded yet. 如果尚未加载,Meteor.user()不一定返回数据。 Meteor.userId() however will return the _id of the user record (if they are logged in). 但是Meteor.userId()将返回用户记录的_id(如果他们已登录)。 If you can change your query to rely on that _id, then it can work. 如果您可以将查询更改为依赖于该_id,那么它可以正常工作。

Depending on which router you are using, you can add a resolve: entry to ensure that the route waits for the user record to be loaded before activating it, and then your query will work. 根据您使用的路由器,您可以添加resolve:条目以确保路由在激活之前等待加载用户记录,然后您的查询将起作用。

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

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