I know this question has been asked numerous times but I am having a difficult time publishing this information.
In Accounts.onCreateUser I am adding a field like so
{
...
user['info'] = { email: options.email, is_admin: false};
}
I publish this information;
Meteor.publish('user', function() {
return Meteor.users.find({}, { fields: { info: 1} });
}
and
Meteor.subscribe('user');
After debugging the Publish query returns the correct information but that is never given to the client when I try to access Meteor.user(). Do I have to do something else to allow info to be access by Meteor.user()?
Any suggestions?
You'll want to use null to publish to the single user.
Meteor.publish(null, function() {
return Meteor.users.find({_id: this.userId}, {fields: {info: 1}});
});
Related
In Meteor, one can add additional fields to the root-level of the new user document like so:
// See: https://guide.meteor.com/accounts.html#adding-fields-on-registration
Accounts.onCreateUser((options, user) =>
// Add custom field to user document...
user.customField = "custom data";
return user;
});
On the client, one can retrieve some data about the current user like so:
// { _id: "...", emails: [...] }
Meteor.user()
By default, the customField does not exist on the returned user. How can one retrieve that additional field via the Meteor.user() call such that we get { _id: "...", emails: [...], customField: "..." }? At present, the documentation on publishing custom data appears to suggest publishing an additional collection. This is undesired for reasons of overhead in code and traffic. Can one override the default fields for Meteor.user() calls to provide additional fields?
You have a couple of solutions that you can use to solve this.
Null Publication
Meteor.publish(null, function () {
if (this.userId !== null) {
return Meteor.users.find({ _id: this.userId }, { fields: { customField: 1 } });
} else {
return this.ready();
}
}, { is_auto: true });
This will give you the desired result but will also result in an additional database lookup.. While this is don't by _id and is extremely efficient, I still find this to be an unnecessary overhead.
2.Updating the fields the Meteor publishes for the user by default.
Accounts._defaultPublishFields.projection = { customField: 1, ...Accounts._defaultPublishFields.projection };
This has to be ran outside of any Meteor.startup blocks. If ran within one, this will not work. This method will not result in extra calls to your database and is my preferred method of accomplishing this.
You are actually misunderstanding the documentation. It is not suggesting to populate and publish a separate collection, just a separate publication. That's different. You can have multiple publications/subscriptions that all feed the same collection. So all you need to do is:
Server:
Meteor.publish('my-custom-user-data', function() {
return Meteor.users.find(this.userId, {fields: {customField: 1}});
});
Client:
Meteor.subscribe('my-custom-user-data');
I want to have client-side access for a certain set of fields for ALL users while I would like to have access to even more fields for the current user only. How do I go about writing publish code to accomplish this?
Right from Meteor documentation:
Meteor.publish("userData", function () {
return Meteor.users.find({_id: this.userId},
{fields: {'other': 1, 'things': 1}});
});
And also:
Meteor.publish("allUserData", function () {
return Meteor.users.find({}, {fields: {'nested.things': 1}});
});
Hope this helps.
As mentioned above, the
Meteor.publish("userData", function () {
return Meteor.users.find({_id: this.userId},
{fields: {'other': 1, 'things': 1}});
});
and
Meteor.publish("allUserData", function () {
return Meteor.users.find({}, {fields: {'nested.things': 1}});
});
publish functions will push the data from the Users collection.
Subscribe with
Tracker.autorun(function () {
Meteor.subscribe("userData");
Meteor.subscribe("allUserData");
});
And the additional data will automatically go into the Users collection and be available in the Meteor.user() object.
My story with that:
I proceeded as documentation says, but encountered with weird behavior.
I had publish function, where I published whole profile and email object for current user (lets say userData) and just some subset for the other users (allUserData).
When I had -
Meteor.subscribe("allUserData");
Meteor.subscribe("userData");
On client side right after user logged in, I've received just allUserData data. Thats mean even for my logged in user (That user couldn't see his own email address). When I refresh browser, bug was fixed and I got properly allUserData for all users except one logged in, which has his proper userData (with mentioned email address).
What is interesting, if I changed the sequence of that subscriptions, bug was fixed.:
Meteor.subscribe("userData");
Meteor.subscribe("allUserData");
Putting into Meteor.autosubscribe(function () { }) doesn't changed anything.
Finally I tried put that subscription into Deps.autorun(function() { }) and explicitly add reactivity and the problem with sequence was resolved..:
Deps.autorun(function() {
Meteor.subscribe("allUserData", Meteor.userId());
Meteor.subscribe("userData", Meteor.userId());
// or
// Meteor.subscribe("userData", Meteor.userId());
// Meteor.subscribe("allUserData", Meteor.userId());
});
In publish function I just replace this.userId with userId from parameter.
With next bug which I encountered was, that I've got secret systemData object in profile user's object and that can see just admins, not regular logged in users. But although correct set publish function with 'profile.systemData': 0 that secret object could see all logged in users which looked into his profile object.
Probably it was because my publish function(s) somehow interfered with publish function in Meteor Account package:
// Publish the current user's record to the client.
Meteor.publish(null, function() {
if (this.userId) {
return Meteor.users.find(
{_id: this.userId},
{fields: {profile: 1, username: 1, emails: 1}});
} else {
return null;
}
}, /*suppress autopublish warning*/{is_auto: true});
Anyway I resolved it with help of method Account.onCreateUser() and adding systemData next to profile object, not into profile.
There starts my other problems :) see Meteor.loginWithPassword callback doesn't provide custom object in User accounts doc
PS: If I knew it at begin, I've put systemData object into special collection.
Upon a user registration, I've added a new field for a Meteor user using React as my front end:
React component:
Accounts.createUser({
email: trimInput(this.state.email),
password: this.state.password,
username: trimInput(this.state.username),
regId: 1, // I need to access this to show/hide fields elsewhere.
});
Server:
// I assume this is how it's done?
Accounts.onCreateUser(function(options, user) {
user['regId'] = options.regId
return user
});
In meteor mongo I can see the newly added field: "regId": "1". Great. Now in browswer console: Meteor.user.find().fetch() does not include the field. Meteor.user().regId returns undefined. Huh?
What is the correct way to approach something like this?
I think previous answer is good, but you should improve in order to remove
Meteor.subscribe("userData");
in every controller by publish automatically.
`Meteor.publish(null, function () {
if (this.userId) {
return Meteor.users.find({_id: this.userId},
{fields: {'regId': 1}});
} else {
this.ready();
}
});`
Based on the documentation, I needed to pub/sub the data:
Server:
Meteor.publish("userData", function () {
if (this.userId) {
return Meteor.users.find({_id: this.userId},
{fields: {'regId': 1}});
} else {
this.ready();
}
});
Client:
Meteor.subscribe("userData");
Now you'll have access to Meteor.user().regId
I have a mobile app in development and I'm transforming one of the collections to get the user last seen time, avatar etc.
PlayerRecord.prototype = {
constructor : PlayerRecord,
getAssociatedUser: function () {
return Meteor.users.findOne( this.user_id );
},
lastSeenFormatted: function () {
var user = this.getAssociatedUser();
return (user && user.last_seen) ? user.last_seen : 'Never';
}
}
My problem is that, if the user last seen returns Never initially but then the user is seen, my string return over there is not updated...obviously.
How would you advise me to handle this situation?
Did you check whether any user had a value for last_seen? This field has to be explicitly published.
According to the Meteor docs (http://docs.meteor.com/#/full/meteor_user):
By default, the current user's username, emails and profile are
published to the client. You can publish additional fields for the
current user with:
// server
Meteor.publish("userData", function () {
if (this.userId) {
return Meteor.users.find({_id: this.userId},
{fields: {'last_seen': 1}});
} else {
this.ready();
}
});
// client
Meteor.subscribe("userData");
In my server folder I have a file where i'm doing the following
Meteor.publish("all_jobs", function(jobs) {
return Jobs.find();
});
Meteor.publish("user_jobs", function(user_id) {
return Jobs.find({userid: user_id}, {sort: {date: -1}});
});
On the user his blogpost page I do the following:
Session.set("userid", Meteor.userId());
Meteor.subscribe("user_jobs", Session.get("userid"));
Template.userJobs.helpers({
jobs: function(){
return Jobs.find();
}
});
On my homepage I would need all the blogpost. So i'm trying to access Meteor.subscribe("all_jobs");
The problem i'm having right now is that all the posts are showing up on my user profile instead of only the posts published by the user.
Any suggestions?
Thanks
When you make multiple subscriptions for the same collection, the results will be merged on the client. After subscribing to all_jobs, the client will now have all of the jobs in its local database until the subscription is stopped. When you go to find some of the jobs in the userJobs template, you will need to indicate which ones you actually are interested in. Modify your helper as follows:
Template.userJobs.helpers({
jobs: function() {
return Jobs.find(userid: Meteor.userId());
}
});
Found the problem. The problem was I was subscribed to all the publications because I just subscribed somewhere random in my code.
I fixed this by adding a waitOn in my route:
waitOn: function() {
return [
Meteor.subscribe("userJobs"),
];
},