Boubakr Chieb
Boubakr Chieb

Reputation: 127

How to select user by ID in Meteor JS?

I'm trying to show a user profile with MeteorJS

Every user has a profile stored in MongoDB, But in the navigator console it shows me only the username, email, and _id fields.

this is my code: in /lib/Router.js

Router.route('/profile',{
  name : "profile",
  data : function(){
    user = Meteor.users.find(Meteor.userId()).forEach(function(user) {
      console.log(user);
    });
    //console.log(user);
  },
  waitOn : function(){
    return Meteor.subscribe("allUsers");
  }
});

/server/Publications.js:

Meteor.publish("allUsers",function(){
  return Meteor.users.find({},{
    fields :{
      username : 1,
      emails : 1,
      profile : 1
    }
  });
});

Upvotes: 0

Views: 1264

Answers (1)

Michel Floyd
Michel Floyd

Reputation: 20227

Your profile route looks a bit funky. It seems to me that you would only need the current user's profile, not all users. I'd write this as follows:

Router.route('/profile',{
  name : "profile",
  data(){
    return Meteor.user();
  },
  waitOn(){
    return Meteor.subscribe("me");
  }
});

/server/Publications.js:

Meteor.publish("me",function(){
  return Meteor.users.find(this.userId,{
    fields :{
      username : 1,
      emails : 1,
      profile : 1
    }
  });
});

Upvotes: 1

Related Questions