0
votes

When I write this in console I get undefined however the data is in the database. What am I missing?

Meteor.users.findOne({_id: this.userId},{fields: {"profile.pastEmployer.name": 1}});
1
It sounds like that particular user hasn't been published. - David Weldon
I can see it using meteortoys:allthings so I assume that means it has been published. - bp123
this.userId refers to the current user doesn't it? - bp123
No, only in pushers and methods. Otherwise you should use Meteor.userId(). - David Weldon
This is doing my head in. I published it just to be sure and now I'm getting object with the userId displaying but not the information I'm asking for - bp123

1 Answers

1
votes

I think what you may want to do is this:

var pastEmployerName = Meteor.user().profile.pastEmployer.name;

Depending on how confident you are in the existence of those nested properties, you may want to use a guard like so:

var profile = Meteor.user().profile;
var pastEmployerName = profile && profile.pastEmployer && profile.pastEmployer.name;

Some things to note:

  1. Use Meteor.userId() to get the current user's id, and Meteor.user() to the the current user's document. In publishers and methods, we use this.userId.

  2. A fields projection (as used in your original question) gives you an object including an _id and the minimal structure to display the specified field(s). In your case, you'd expect to get an object with an _id and a profile, which in turn contains a pastEmployer and so on. In general, fields projections are beneficial on the server (they save on bandwidth and CPU), but are of limited use on the client because the complete documents are already in memory.