【问题标题】:Meteor Iron Router error : has no method 'go'Meteor Iron Router 错误:没有“go”方法
【发布时间】:2016-09-24 04:51:36
【问题描述】:

在服务器端方法中将记录插入集合后,我路由到不同的命名路由。但我得到一个错误:“没有方法'go'”。

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

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

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

I20160526-12:00:15.662(3)?异步函数回调异常: TypeError: Object function router(req, res, next) {

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

I20160526-12:00:15.662(3)? //XXX 这假设没有其他路由器在 我们可能应该修复的父堆栈

I20160526-12:00:15.662(3)?请求:请求,

I20160526-12:00:15.663(3)? },下一个);

I20160526-12:00:15.662(3)?回复:res

I20160526-12:00:15.663(3)? } 没有方法'go'

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

【问题讨论】:

    标签: exception meteor iron-router


    【解决方案1】:

    谢谢拉米尔。

    最后我发现是在服务器端找不到库。我还发现了 AutoForm 钩子——这是一种运行插入后代码的更智能的方法。

    我将这个钩子连接到 Iron Route(确切地说是 Iron Route 控制器)

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

    【讨论】:

      【解决方案2】:

      您可能在共享区域(例如lib目录)上定义了方法,因此在客户端它可以正常工作,但在服务器端没有Router.go这样的功能。

      您应该从方法返回结果,然后在客户端端代码上调用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');
          }
      });
      

      【讨论】:

      • 谢谢 Ramil 我感觉它在服务器端不可用。但是 Meteor 方法是暴露给客户端的,所以我认为它可能会起作用。
      猜你喜欢
      • 2015-01-25
      • 2015-07-07
      • 2014-10-23
      • 2015-06-26
      • 2014-05-07
      • 2013-11-21
      • 1970-01-01
      • 2023-03-12
      • 2017-06-17
      相关资源
      最近更新 更多