How to public custom field in Users Collection?

Config to use custom field in Users Collection

import {Accounts} from 'meteor/accounts-base';

// Add new custom field
Accounts.onCreateUser(function (options, user) {
    user.profile = options.profile;
    user.roleGroup = options.roleGroup;

    return user;
});
-------
// public
// Customer field of users
Meteor.publish(null, function () {
    if (!this.userId) {
        return this.ready();
    }

    return Meteor.users.find({_id: this.userId}, {fields: {roleGroup: 1}});
});
---------
// Client
console.log(Meteor.user()); // can not get `roleGroup field`???
1 Like

Hello @theara ,

Although null publications should be published to every connected Client, have you tried to use named pub/sub?
From Meteor Users docs:

Meteor.publish('user.custom', function () {
    if (!this.userId) {
        return this.ready();
    }
    
    // For testing purpose, make sure roleGroup exists on this user' record
    console.warn(Meteor.users.findOne({_id: this.userId}, {fields: {roleGroup: 1}}));
    return Meteor.users.find({_id: this.userId}, {fields: {roleGroup: 1}});
});

// Client
Meteor.subscribe('user.custom');

Thanks for your helping :+1: