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

时间:2017-06-22 20:11:26

标签: javascript mongodb meteor

PAGE CUSTOMERS:列出users集合中的所有用户。 PAGE PROFILE:列出仅登录的用户配置文件信息。

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

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

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;
  },
});

个人资料页面根本没有显示任何信息。如何让它只显示登录的用户信息?谢谢!

1 个答案:

答案 0 :(得分:0)

要获取当前登录的用户,您可以使用:Meteor.user()

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