简体   繁体   English

订阅同时列出所有用户和this.userId的Meteor.users()

[英]Subscribe to Meteor.users() both listing all users and this.userId

PAGE CUSTOMERS: Lists all users in the users collection. 页面客户:列出用户集合中的所有用户。 PAGE PROFILE: List only logged in user profile information. 页面配置文件:仅列出已登录的用户配置文件信息。

userProfiles.js: userProfiles.js:

 if (Meteor.isServer) {
   Meteor.publish("userData", function () {
    return Meteor.users.find({}, {
        fields: {
          // VISIBLE
          'profile.mobile': 1,
          'profile.zipcode': 1,
          'profile.first_name': 1,
          'profile.work_title': 1,
          'emails[0].address': 1,
        }});
    });
 }

profile.js profile.js

Template.profileDetails.helpers({
  user: function() {
    return Meteor.users.find({_id: this.userId});
  },

  userEmail: function() {
    return this.emails[0].address;
  },

  userFirstName: function() {
    return this.profile.first_name;
  },

  userTitle: function() {
    return this.profile.work_title;
  },

  userMobile: function() {
    return this.profile.mobile;
  },

  userZip: function() {
    return this.profile.zipcode;
  },
});

customers.js customer.js

Template.customerDetails.helpers({
  user: function() {
    return Meteor.users.find();
  },

  userEmail: function() {
    return this.emails[0].address;
  },

  userFirstName: function() {
    return this.profile.first_name;
  },

  userTitle: function() {
    return this.profile.work_title;
  },

  userMobile: function() {
    return this.profile.mobile;
  },

  userZip: function() {
    return this.profile.zipcode;
  },
});

The profile page is not showing any information at all. 个人资料页面根本没有显示任何信息。 How can i get it to only display the logged in user information? 我如何才能只显示已登录的用户信息? Thank you! 谢谢!

the "this" in the helpers isn't the user. 助手中的“ this”不是用户。 since you're looking for the current user in your profile template, you can do it in Blaze, without a helper: 由于您要在个人资料模板中查找当前用户,因此无需帮助者即可在Blaze中进行操作:

{{currentUser.profile.first_name}}

for the customers, you can loop over the users returned by your helper. 对于客户,您可以遍历助手返回的用户。 i would rename the helper: 我将重命名该助手:

Template.customerDetails.helpers({
  customers(){
    return Meteor.users.find({});
  }
});

then you can loop over them, in Blaze, like this: 然后您可以像这样在Blaze中循环播放它们:

{{#each customer in customers}}
  {{customer.profile.first_name}}
{{else}}
   No customers found.
{{/each}}

note that you don't need any other helpers to make that work. 请注意,您不需要任何其他帮助者即可完成该工作。

cf http://blazejs.org/guide/spacebars.html#Each-in cf http://blazejs.org/guide/spacebars.html#Each-in

To get the currently logged-in user, you could use: Meteor.user() : 要获取当前登录的用户,可以使用: Meteor.user()

//...
user: function() {
  return Meteor.user();
},
//...

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

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