Meteor Iron Router error : has no method 'go'

Meteor Iron Router error : has no method 'go'

在服务器端方法中将记录插入集合后,我路由到不同的命名路由。但是我得到一个错误:"has no method 'go'".

Meteor.methods({
  'create_item': function (item) {

    Items.insert(item, function (error,result){
      if(result){
        Router.go('dashboard');
      }
    });
  },
});

路由更改成功,页面呈现仪表板模板,但出现以下错误。

I20160526-12:00:15.662(3)? Exception in callback of async function: TypeError: Object function router(req, res, next) {

I20160526-12:00:15.662(3)? router.dispatch(req.url, {

I20160526-12:00:15.662(3)? //XXX this assumes no other routers on the parent stack which we should probably fix

I20160526-12:00:15.662(3)? request: req,

I20160526-12:00:15.663(3)? }, next);

I20160526-12:00:15.662(3)? response: res

I20160526-12:00:15.663(3)? } has no method 'go'

I20160526-12:00:15.663(3)? at lib/methods.js:17:16

你可能在共享区域(例如lib目录)定义了方法,所以在客户端它工作正常,但在服务器端没有Router.go.[=15这样的功能=]

您应该 return 从方法中得到结果,然后在 客户端 端代码上调用 Router.go

在服务器上:

Meteor.methods({
    'create_item': function (item) {
        //Insert blocks on server side,
        //no need to use callback
        return Items.insert(item);
    },
});

在客户端:

Meteor.call('create_item', item, function(err, res) {
    if (err) {
        console.error(err);
    } else {
        Router.go('dashboard');
    }
});

谢谢拉米尔。

最后发现是服务端找不到lib。我还发现了 AutoForm 挂钩 - 这是 运行 post 插入代码的一种更智能的方法。

我将这个挂钩连接到 Iron Route(确切地说是 Iron Route Controller)

onRun: function () {
    AutoForm.hooks({
      createItemForm: {
        onSuccess: function(){
         Router.go('dashboard');
        }
      }
    });
    this.next();
  },