Reputation: 84
What's happening is, when .addGuest is clicked everything works except that meal becomes undefined and my view is broken. Everything that is revealed by {{#if owner}} does not get displayed and I get this error in my console.
Exception in template helper: TypeError: Cannot read property 'owner' of undefined
at Object.Template.menu_details.helpers.owner (http://localhost:3000/client/workflow/meal_details_page/meal_details.js
There are no errors on my server console and all data being passed is stored in my db. Somehow Session.get('current_meal') is losing it's data on the invite method. I had commented it out of the event handler and everything else works perfectly.
Thanks
this is my Query that is getting lost...
meal: function () {
return MealModel.findOne(Session.get('current_meal'));
},
owner: function() {
var meal = MealModel.findOne(Session.get('current_meal'));
return meal.owner === Meteor.userId();
},
here is my method in the server
invite: function (options) {
check(options, {
mealId: String,
firstName: String,
lastName: String,
email: String
});
var meal = MealModel.findOne(options.mealId);
if (! meal || meal.owner !== this.userId)
throw new Meteor.Error(404, "No such meal");
MealModel.update(options.mealId, {$addToSet: {
invited: {
firstName: options.firstName,
lastName: options.lastName,
email: options.email
}}});
},
my Meteor.call function...
invite = function(options) {
Meteor.call('invite', options);
},
and finally, my event handler...
'click .addGuest': function (evt, tmpl) {
var firstName = tmpl.find('.firstName').value;
var lastName = tmpl.find('.lastName').value;
var email = tmpl.find('.email').value;
var meal = Session.get('current_meal');
if (firstName.length && lastName.length && email.length) {
var id = createGuest({
firstName: firstName,
lastName: lastName,
email: email,
meal: meal._id,
owner: meal.owner
});
invite({
firstName: firstName,
lastName: lastName,
email: email,
mealId: meal._id
});
};
return false;
},
Upvotes: 1
Views: 147
Reputation: 7366
The code that's missing from your question is where you set the session variable current_meal
. You have to be careful with findOne
because it returns null
when no documents are found—and that can happen both when there really are no matching documents in the collection, but also if you’re on the client and the matching documents simply haven’t synced to the client yet.
To fix it, find your code that says Session.set('current_meal', meal)
or similar, and add a check so that it only happens when meal
isn’t null:
if (meal != null)
Session.set('current_meal', meal);
Also, in your click .addGuest
event handler, check that meal
is valid like you also check the other values:
if (firstName.length && lastName.length && email.length && meal != null) {
Upvotes: 2