bp123
bp123

Reputation: 3417

Console returning undefined

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}});

Upvotes: 0

Views: 72

Answers (1)

David Weldon
David Weldon

Reputation: 64312

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.

Upvotes: 1

Related Questions