Exception in the template helper: TypeError: Unable to read property profile 'undefined

Now I had a similar problem before I got this error:

Exception in the template helper: TypeError: cannot read property 'profile' undefined

The same thing happens again, but in a second order, which contains other user profile information (the first profile is determined). How do I get it to be reprocessed in {{#each orders}}?

It also seems that info.firstName, lastName and building are 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!

+4
source share
1 answer

. undefined. info , user . :

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

, undefined.

, autopublish. , / / Meteor.users , , minimongo.

Meteor.users :

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

Meteor.subscribe("users");

Meteor.users Meteor.user

+15

All Articles