4

Now I had a similar problem before where I was getting this error:

Exception in template helper: TypeError: Cannot read property 'profile' of undefined

The same thing is happening again but on the second order, which contains another users profile information (the first profile is defined). How would I get it to re-render in {{#each orders}}?

It also appears that info.firstName, lastName, and building gets called 3 times for some reason when there are only 2 orders...

In HTML:

<template name="orderItem">
  <section>
    <form role="form" id="ordersList">
      <div>
        {{#each orders}}
          <input type="text" name="name" value="{{info.firstName}} {{info.lastName}}">
        {{/each}}
      </div>
      <div>
        {{#each orders}}
          <input type="text" name="building" value={{info.building}}>
        {{/each}}
      </div>
      <div>
        {{#each orders}}
          <input type="text" name="featuredDish" value={{featuredDish}}>
        {{/each}}
      </div>
    </form>
  </section>
</template>

In javascript:

Template.orderItem.orders = function() {
  var todaysDate = new Date();
  return Orders.find({dateOrdered: {"$gte": todaysDate}});
};

Template.orderItem.info = function() {
  var userId = this.userId;
  var user = Meteor.users.findOne(userId)
  var firstName = user.profile.firstName;
  var lastName = user.profile.lastName;
  var building = user.profile.building;

  return {
    firstName: firstName,
    lastName: lastName,
    building: building
  }
};

Appreciate the help!

Jason Chung
  • 127
  • 1
  • 3
  • 9
  • Add semicolon at the end in **var user = Meteor.users.findOne(userId);** – Swetha Sep 10 '14 at 06:23
  • Are you still using auto-publish or are you setting up a correct publication for the Meteor.users collection ? Be aware that Meteor automatically sets up a publication which only publish the username of the currently logged in user : this is not going to be enough for what you need. – saimeunt Sep 10 '14 at 07:20

1 Answers1

15

This error is common issue. You are trying to access user object which is undefined. Function info doesn't check if user is correct object. Use technique called guarding :

Template.orderItem.info = function() {
  var userId = this.userId;
  var user = Meteor.users.findOne(userId)

  var firstName = user && user.profile && user.profile.firstName;
  var lastName = user && user.profile  && user.profile.lastName;
  var building = user && user.profile  && user.profile.building;

  return {
    firstName: firstName,
    lastName: lastName,
    building: building
  }
};

Above code won't throw any error even if user is undefined.

I assume that you have removed autopublish package. Probably you haven't published/subscribed from/to Meteor.users collection, so there is no data to find in minimongo.

Remember to publish Meteor.users collection and subscribe to it:

Meteor.publish("users", function(){
  return Meteor.users.find({},{fields:{profile:1}})
})

Meteor.subscribe("users");

Publish certain information for Meteor.users and more information for Meteor.user

Community
  • 1
  • 1
Kuba Wyrobek
  • 5,273
  • 1
  • 24
  • 26
  • How would guarding help define 'profile'. The above code gets rid of the error, but doesn't solve the profile from being undefined. Are you saying that you can't pull another users profile info from "Meteor.users" unless they are logged in? – Jason Chung Sep 10 '14 at 07:15
  • If you publish documents from Meteor.users collection then every profile can be accessed. By default, without publish user can access only his profile. – Kuba Wyrobek Sep 10 '14 at 07:44
  • I believe that Meteor timing may be trying to load the variable into the template before the variable is defined, so guarding will at least suppress the error and keep your console uncluttered. In the meantime, Meteor's reactive nature will eventually load the variable and populate the template correctly. – Thor Jan 30 '15 at 03:51