简体   繁体   中英

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:

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

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. since you're looking for the current user in your profile template, you can do it in Blaze, without a helper:

{{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:

{{#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

To get the currently logged-in user, you could use: Meteor.user() :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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