【问题标题】:Using Iron Router on Server Side of Meteor Application?在 Meteor 应用程序的服务器端使用 Iron Router?
【发布时间】:2015-03-07 09:42:58
【问题描述】:

一旦信息被插入数据库,我尝试调用 Router.go('confirmation') 将用户带到确认页面。

Meteor.methods({
    'createNewItinerary': function(itinerary){
      var userId = Meteor.userId();
      ItineraryList.insert({
        [....values.....]
      },function(){
        Router.go('confirmation'); 
      });

    }

在服务器控制台中,我得到响应:has no method 'go'

数据插入成功,如何让它路由到确认页面?

-- 编辑--

这行得通吗?好像但是不知道怎么验证:

Meteor.call('createNewItinerary',itinerary, function(err, data){
       if(err){
         console.log(err);
       }
       else Router.go('confirmation');
     });

【问题讨论】:

  • 如果插入没有返回任何错误,请转到确认页面
  • 这行得通吗? Meteor.call('createNewItinerary',itinerary, function(err, data){ if(err){ console.log(err); } Router.go('confirmation'); });
  • router.go 应该是 'else' 块
  • 已编辑以将 Router.go 放入 else 块中!谢谢@yoK0

标签: mongodb meteor iron-router


【解决方案1】:

您的建议是不错的选择。 您无法在 Meteor 方法中捕获路由器,因为它是服务器端。您必须在 回调函数 中执行此操作,就像您建议的那样:

Meteor.call('createNewItinerary',itinerary, function(err, data){
   if(err){
     console.log(err);
   }
   Router.go('confirmation');
 });

要检查工作是否在服务器上正确完成,只需抛出错误,例如:

 throw new Meteor.Error( 500, 'There was an error processing your request' );

然后如果抛出错误,它将记录在您的客户端。

希望对你有帮助:)

【讨论】:

    【解决方案2】:

    你的建议对我来说很有意义:

    Meteor.call('createNewItinerary',itinerary, function(err, data){
       if(err){
         console.log(err);
       }
       Router.go('confirmation');
     });
    

    您将调用 createNewItinerary,当它返回时,您会将用户发送到确认页面。也就是说,您可能需要一些错误检查 - 因为您目前已经获得它,所以无论插入成功还是失败,您都将用户发送到确认页面。也许:

    Meteor.call('createNewItinerary',itinerary, function(err, data){
       if(err){
         console.log(err);
         Router.go('errorpage'); // Presuming you have a route setup with this name
       }
       else Router.go('confirmation');
     });
    

    【讨论】:

      猜你喜欢
      • 2014-03-27
      • 2013-09-03
      • 2016-08-26
      • 2015-02-27
      • 1970-01-01
      • 2015-02-23
      • 2016-03-31
      • 2018-07-12
      • 2017-06-17
      相关资源
      最近更新 更多