Users collection how to expose new field


#1

I have added new field in user collection but when I access it in the client side Meteor.user().cart ( cart is my new field). Its undefined. How can expose new field (cart) so that it can be access in the client?


#2

You need to publish the data to the user.

// -- this belongs in /server/ directory --
// Publish user data
Meteor.publish(null, function () {
  if (this.userId) {
    return Meteor.users.find({_id: this.userId}, {fields: {cart: 1}});
  }else{
    this.ready();
  }
});

When publishing with the title null its auto subscribed by clients.


#3

Will this really work? The user is not necessary logged in in the beginning, and the subscription is not updated when the user do log in.

I think you need to subscribe to the extra field from the client each time the user logs in.


#4

Yeah it will work, I pulled that code from my app.
I use it to share a couple user flags (is_admin etc…).

Quote from the docs:

Name String
Name of the record set. If null, the set has no name, and the record set is automatically sent to all connected clients.

If the user isn’t logged in, then the subscription returns publish just returns this.ready() so as to not hold anything up.


#5

But what happens when a new user signs up? He won’t get the extra fields, will he?


#6

Oh I see what you’re saying. @ccfiel only asked how to expose the new field. I assumed that the field is being added when users are signed up.


#7

@cstrat Thanks for the code! :slight_smile:


#8

I don’t think you do :stuck_out_tongue:

When a non member comes to the page, he subscribes to the null publish. He’s not signed in, so nothing happens. When he later signs up, the null publish needs to be re-run for that client, otherwise he’ll not have access to the custom fields. You’re with me?


#9

Maybe keep the cart as a local Collection when no one is signed in?


#10

I am confused. When no user is signed in, there is no user record. So how are you expecting to expose a new field to an anonymous user when there is no record?

It sounds to me that you need to create a cart outside of the user collection and tie it to a uniq “session id” or create a localStorage for anon users that gets synced with the user account once authenticated.


#11

@Peppe_LG this.userId is special in publishers. From the docs:

This is constant. However, if the logged-in user changes, the publish function is rerun with the new value.

So yes, it works.


#12

Thanks @dweldon, I just learned something new :grinning:


#13

I have added “settings” new field in create new account. I am using meteor account package for user management.

  1. If I added “settings” into the profile, it will return me to client site using Meteor.user()
  2. If I added “settings” separately in user collection. I will want available to client side.
    I have publish this “seetings” field on server
    users.find({’_id’: {$ne : uid}}, {fields: {settings: 1}});

Can you please suggest solution for 2nd option.
Thanks in advance.