Console returning undefined

前端 未结 1 1398
心在旅途
心在旅途 2021-01-28 21:31

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.         


        
相关标签:
1条回答
  • 2021-01-28 21:57

    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.

    0 讨论(0)
提交回复
热议问题