【问题标题】:Meteor: Publishing all user not working without autopublish packageMeteor:发布所有用户在没有自动发布包的情况下无法工作
【发布时间】:2015-07-09 01:33:59
【问题描述】:

我想在我的模板中显示所有用户的列表。 我有:

//publications.js
Meteor.publish('users', function() {
    return Meteor.users.find({}, { fields: {username: 1, profile: 1} });
});

//router.js
Router.route('/users/add/:_id?', {name: 'users.add', controller: 'UserAddController'});

UserAddController = RouteController.extend({
  subscriptions: function(){
    return [ Meteor.subscribe('hospitals'), 
            Meteor.subscribe('roles'),
            Meteor.subscribe('users') ];
  },
  action: function() {
    this.render('addUser', { 
      data: function(){
        return { hospital_id : this.params._id }
      }
    });
  }
});


//client
Template.listUsers.helpers({
  users: function() {
    return Meteor.users.find({});
  }
});

但列表只显示当前登录的用户。我使用Account.createUser() 函数创建了一个用户列表我做错了什么?

谢谢。

【问题讨论】:

  • 您的 Publications.js 文件位于何处?它应该在服务器目录中。
  • 是的,它在服务器目录中。
  • 检查您的模板。还要在浏览器控制台中执行Meteor.users.find().fetch(),看看它是否拥有所有用户。
  • 在控制台中,一直只显示一个,登录用户。

标签: meteor meteor-accounts meteor-publications


【解决方案1】:

你必须使用subscriptions钩子中的this.subscribe()订阅一个发布:

// a place to put your subscriptions
subscriptions: function() {
  this.subscribe('items');

  // add the subscription to the waitlist
  this.subscribe('item', this.params._id).wait();
}

或者使用waitOn:

// Subscriptions or other things we want to "wait" on. This also
// automatically uses the loading hook. That's the only difference between
// this option and the subscriptions option above.
waitOn: function () {
  return Meteor.subscribe('post', this.params._id);
}

【讨论】:

    【解决方案2】:

    默认情况下,Meteor 发布当前用户。我看到你有一个addUser 模板和一个listUsers 模板。问题是addUser 订阅了users 发布,listUsers 不是(这当然取决于您的路由器中还有什么)。要解决此问题,请将调用更改为 this.render 以呈现 listUsers 模板。然后,您的 users 助手应该可以工作了,您可以随意呈现信息。

    我用我自己的应用程序(DiscoverMeteor 的显微镜项目)对此进行了测试,它对我有用。希望它也适合你。如果没有,请在此处评论,如果有效,请务必接受此答案。 =)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-03
      • 2015-03-05
      • 2015-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      相关资源
      最近更新 更多