对象#<对象>没有方法'用户'

Object #<Object> has no method 'User'

本文关键字:对象 用户 有方法 gt lt      更新时间:2023-09-26

每个人。我的代码和Meteor 0.9.4 有问题

这是我的代码:

服务器/publications.js

Meteor.publish('getUsers', function () {
    var loggedInUser = Meteor.User();
    if (Roles.userIsInRole(loggedInUser, ['admin'])) {     
        return Meteor.users.find({}, {fields: {
            _id: 1,
            emails: 1,
            roles: 1
        }});
    }
    this.stop();
    return;
});

Lib/router.js

Router.map(function() {
    this.route('dashboardUsers', {
        layoutTemplate: 'dashboardLayout',   
        path: "/dashboard/users",
        waitOn: function() {
            return Meteor.subscribe('getUsers');
        }
    });    
});

当我运行流星应用程序时,我有以下错误:

=> App running at: http://localhost:3000/
I20141019-18:21:50.827(4)? Exception from sub 8CRiG3Jmdv4mohPhd TypeError: Object #<Object> has no method 'User'
I20141019-18:21:50.949(4)?     at null._handler (app/server/publications.js:3:31)
I20141019-18:21:50.950(4)?     at maybeAuditArgumentChecks (packages/ddp/livedata_server.js:1594)
I20141019-18:21:50.950(4)?     at _.extend._runHandler (packages/ddp/livedata_server.js:943)
I20141019-18:21:50.950(4)?     at _.extend._startSubscription (packages/ddp/livedata_server.js:769)
I20141019-18:21:50.951(4)?     at _.extend.protocol_handlers.sub (packages/ddp/livedata_server.js:582)
I20141019-18:21:50.951(4)?     at packages/ddp/livedata_server.js:546

如果你查看Meteor.user的文档,你会发现它在的任何地方都有效,发布函数除外。你需要做:

var loggedInUser = Meteor.users.findOne(this.userId);

您得到的错误是因为Meteor.User中的大写'u',它应该是Meteor.user

但是,正如David Weldon所指出的,您需要在发布函数中使用Meteor.users.findOne(this.userId)

对于其他在尝试访问Meteor.user()时遇到错误Object #<Object> has no method 'user'的人,请确保您拥有accounts基本包。