Reputation: 127
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!
Upvotes: 4
Views: 10865
Reputation: 5273
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
Upvotes: 15