Limit Meteor.user() Profile fields


#1

Hello,

My user collection looks like this:

{
    emails: [{…}]
    profile: {
      firstName: "Faisal Ali",
      lastName: "Sayyed", 
      pic: "s3Url",
      gender: 'male',
      products: []
      createdAt: timeStamp
   }
  username: "FaisalAli19"
  _id: "rCaB2GsNXNnc4bC3R"
}

I am trying to limit the user profile to this fields firstName, lastName, pic, and gender.

I tried the below publish methods but Meteor.user() still return all the fields.

Meteor.publish(null, function() {
  if (!this.userId) return;
  return Meteor.users.find(
    { _id: this.userId },
    {
      fields: {
        "profile.pic": 1,
        "profile.firstName": 1,
         "profile.lastName": 1,
         "profile.gender": 1
      }
    }
  );
});

and

Meteor.publish('userData', function() {
  if (!this.userId) return;
  return Meteor.users.find(
    { _id: this.userId },
    {
      fields: {
        "profile.pic": 1,
        "profile.firstName": 1,
         "profile.lastName": 1,
         "profile.gender": 1
      }
    }
  );
});

// on client side

Tracker.autorun(() => {
    Meteor.subscribe('userData')
})

Any help will be appriciated thanks.


#2

Can you please wrap your code in triple-backticks:

```
like
this
```

Thank you :slight_smile:


#3

Sure. Just did that.


#4

By default, Meteor publishes everything in the profile of the connected user. Not sure if you can override that. Better not to use the profile fields at all in my opinion, just create your own and work from there, you’ll get the expected behavior that way.


#5

@pdecrat

Yeah if I move the product and createdStamp outside from profile then by default Meteor will not send those fields to clients. And my issue will get resolved.

Thanks, man :+1: