我是Meteor的新手,我正在尝试使用Meteor.call()将对象推送到我的集合中的数组。这是我的代码。
我的模板活动地图
'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
});
和我在共享文件夹中的model.js,
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}}});
},
我本来可以创建一个名为Menu的相关集合并设置{owner:mealId},但我真的想在MongoDB上运行嵌入式文档。任何意见都将不胜感激。
答案 0 :(得分:5)
我猜测问题是mealId
方法范围内没有addToMenu
变量。您可能打算将其作为参数传递:
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});
}
}