I am new to Meteor and am trying to use a Meteor.call() to push an object to an array in my collection. Here is my code.
My template events map
'click .save': function (event, template) {
var mealId = Session.get('selected');
var category = template.find(".category").value;
var dish = template.find(".dish").value;
if (category.length && dish.length) {
addToMenu({
category: category,
dish: dish
});
and my model.js in the shared folder,
addToMenu = function(options) {
var id = Random.id();
Meteor.call('addToMenu',_.extend({ _id: id}, options));
return id;
};
Meteor.methods({
createMeal: function(options) {
check(options, {
date: String,
time: String,
street: String,
city: String,
state: String,
zipcode: String,
_id: Match.Optional(String)
});
if (options.street.length > 100)
throw new Meteor.Error(413, 'Street address too long');
if (options.city.length > 25)
throw new Meteor.Error(413, 'City name too long');
if (options.state.length > 20)
throw new Meteor.Error(413, 'State name too long');
if (! this.userId)
throw new Meteor.Error(403, 'You must be logged in');
var id = options.id || Random.id();
Meals.insert({
_id: id,
owner: this.userId,
street: options.street,
city: options.city,
state: options.state,
zipcode: options.zipcode,
date: options.date,
time: options.time,
menu: [],
ingredients: [],
invited: [],
rsvps: []
});
return id;
},
addToMenu: function(options) {
check(options, {
category: String,
dish: String,
_id: Match.Optional(String)
});
if (! this.userId)
throw new Meteor.Error(403, "You must be logged in to add dishes.");
if (! mealId)
throw new Meteor.Error(404, "No such meal");
Meals.update(mealId, {$addToSet: {menu: {category: options.category, dish:
options.dish}}});
},
I could have just created a related collection called Menu and set {owner: mealId} but I really wanted to run this exercise of embedded documents on MongoDB. Any input would be greatly appreciated.
I am new to Meteor and am trying to use a Meteor.call() to push an object to an array in my collection. Here is my code.
My template events map
'click .save': function (event, template) {
var mealId = Session.get('selected');
var category = template.find(".category").value;
var dish = template.find(".dish").value;
if (category.length && dish.length) {
addToMenu({
category: category,
dish: dish
});
and my model.js in the shared folder,
addToMenu = function(options) {
var id = Random.id();
Meteor.call('addToMenu',_.extend({ _id: id}, options));
return id;
};
Meteor.methods({
createMeal: function(options) {
check(options, {
date: String,
time: String,
street: String,
city: String,
state: String,
zipcode: String,
_id: Match.Optional(String)
});
if (options.street.length > 100)
throw new Meteor.Error(413, 'Street address too long');
if (options.city.length > 25)
throw new Meteor.Error(413, 'City name too long');
if (options.state.length > 20)
throw new Meteor.Error(413, 'State name too long');
if (! this.userId)
throw new Meteor.Error(403, 'You must be logged in');
var id = options.id || Random.id();
Meals.insert({
_id: id,
owner: this.userId,
street: options.street,
city: options.city,
state: options.state,
zipcode: options.zipcode,
date: options.date,
time: options.time,
menu: [],
ingredients: [],
invited: [],
rsvps: []
});
return id;
},
addToMenu: function(options) {
check(options, {
category: String,
dish: String,
_id: Match.Optional(String)
});
if (! this.userId)
throw new Meteor.Error(403, "You must be logged in to add dishes.");
if (! mealId)
throw new Meteor.Error(404, "No such meal");
Meals.update(mealId, {$addToSet: {menu: {category: options.category, dish:
options.dish}}});
},
I could have just created a related collection called Menu and set {owner: mealId} but I really wanted to run this exercise of embedded documents on MongoDB. Any input would be greatly appreciated.
Share Improve this question asked Aug 1, 2014 at 1:58 Chad_MartinsonChad_Martinson 841 gold badge1 silver badge8 bronze badges 2- 1 What do you get on your server side console when you try this? – Tarang Commented Aug 1, 2014 at 7:19
- It says mealId is not defined. Didn't even think to look at the server console. Thanks – Chad_Martinson Commented Aug 2, 2014 at 3:47
1 Answer
Reset to default 5I'm guessing the problem is that there's no mealId
variable in scope in the addToMenu
method. You probably meant to pass it as a parameter:
Meteor.methods({
addToMenu: function(mealId, options) {
check(mealId, String);
// rest of function body unchanged
}
});
addToMenu = function(mealId, options) {
var id = Random.id();
Meteor.call('addToMenu', mealId, _.extend({ _id: id}, options));
return id;
};
'click .save': function (event, template) {
var mealId = Session.get('selected');
var category = template.find(".category").value;
var dish = template.find(".dish").value;
if (category.length && dish.length) {
addToMenu(mealId, {category: category, dish: dish});
}
}