1

我有一个流星发布功能,我在该发布功能中抛出一个错误,以被订阅者捕获。我正在使用 Iron-router,并在“waitOn”功能中订阅该出版物。由于某种原因,当我从出版物中抛出错误时,我的错误函数没有被调用,我不确定为什么。非常感谢您的帮助!

这是我的订阅路线 (to 'assignmentsByGroup') 和我的错误函数 ( onErrorfunction):

this.route('assignmentsList',
{path: '/groups/:groupId',
  waitOn: function() {
  var onErrorfunction = function(error, result)
  {
    console.log("onErrorfunction called");
    if(error)
    {
      console.log("Error!");
      alert(error.reason);
    }
  };
  return [Meteor.subscribe('assignmentsByGroup', this.params.groupId, onErrorfunction), Meteor.subscribe("groupById", this.params.groupId)];
  },
  data: function() {
    return {
      groupId: this.params.groupId
    }
  }
}
);

这是我的发布功能:

Meteor.publish("assignmentsByGroup", function(groupId)
{
try
{
  if(_.contains(Groups.findOne({_id: groupId}).members, this.userId))
    {
        return Assignments.find({group: groupId});
    }
    else
    {
    var errorToThrow = new Meteor.Error(401, "Access denied: you cannot view assignments unless you are a member of this group.");
        this.error(errorToThrow);
    }
}
catch(err)
{
  this.error(err);
}
});
4

2 回答 2

4

问题是我的 onError 回调语法。我给订阅方法传递了一个函数,它被解释为一个 onReady 函数,只有在发布函数中调用 this.ready() 时才会调用。我改成Meteor.subscribe('assignmentsByGroup', this.params.groupId, onErrorfunction)Meteor.subscribe('assignmentsByGroup', this.params.groupId, {onError: onErrorfunction})它工作!此外,您仍然可以在发布函数中使用 try 和 catch 语句;不管有没有它,错误仍然会正确抛出,但有了它,您还可以捕获其他内部服务器错误。

于 2014-02-19T02:29:35.760 回答
0

删除 try/catch 块。您已经在一个返回发布或错误的函数中。

当您执行 try/catch 时,您抛出的实际错误(Meteor.Error)被捕获并作为常规 javascript 错误传递/抛出到 catch 中。

但订阅正在等待适当的 Meteor.Error

于 2014-02-17T22:21:44.913 回答