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

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

本文关键字:控制器 未定义 客户端 user Meteor      更新时间:2023-09-26

在METEORJS应用程序的客户端,我有一个显示一些用户的控制器。

我有一个问题与流星.user()函数,错误是:流星.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:条目来确保路由在激活它之前等待用户记录被加载,然后你的查询就会工作。

我有一个类似的问题,错误是Meteor.user()不是一个函数

在我的例子中,我需要通过执行

来安装Meteor包accounts-password。

meteor add accounts-password