javascript Meteor.call 正在返回错误调用方法“addToMenu”:内部服务器错误 [500]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25071730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:52:06  来源:igfitidea点击:

Meteor.call is returning Error invoking Method 'addToMenu': Internal server error [500]

javascriptmeteor

提问by Chad_Martinson

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.

我是 Meteor 的新手,正在尝试使用 Meteor.call() 将对象推送到我的集合中的数组。这是我的代码。

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,

和我在共享文件夹中的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}}});
},

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.

我本来可以创建一个名为 Menu 的相关集合并设置 {owner:mealId} 但我真的很想在 MongoDB 上运行这个嵌入文档的练习。任何投入将不胜感激。

回答by user3374348

I'm guessing the problem is that there's no mealIdvariable in scope in the addToMenumethod. You probably meant to pass it as a parameter:

我猜问题是方法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});
  }
}