服务器端路由方法中的会话对象 - 流星

时间:2015-02-09 09:21:06

标签: meteor iron-router

运行我的应用程序并尝试路由到root / about

时出现此错误
ReferenceError: Session is not defined at [object Object].route.onBeforeAction (app/lib/routes.js:38:8) at boundNext (packages/iron:middleware-stack/lib/middleware_stack.js:251:1) at runWithEnvironment (packages/meteor/dynamics_nodejs.js:108:1) at packages/meteor/dynamics_nodejs.js:121:1 at [object Object].urlencodedParser (/home/action/.parts/packages/meteor/1.0/packages/iron_router/.1.0.7.42k4wv++os+web.browser+web.cordova/npm/node_modules/body-parser/lib/types/urlencoded.js:72:36) at packages/iron:router/lib/router.js:277:1 at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1) at [object Object].hookWithOptions (packages/iron:router/lib/router.js:276:1) at boundNext (packages/iron:middleware-stack/lib/middleware_stack.js:251:1) at runWithEnvironment (packages/meteor/dynamics_nodejs.js:108:1) 

这是我在这个位置的路由代码lib / routes.js:

Router.map(function () {
     this.route('aboutTemplate', {
        name: 'aboutTemplate',
        where: 'server',
        path: '/about',
         onBeforeAction: function(){
           Session.set('active_menu_option','about');
           this.next();
         },
         onAfterAction: function(){

        },
        data: function(){
          return {active_menu_option: {'about':'active'}};
        }
      });
}

我认为发生错误是因为这现在是服务器端路由,并且会话对象在客户端范围之外不可用。但有没有人可以给我这方面的信息呢?

同样,当我在它的时候 - 在上面的方法中代表的第一个参数是什么?第一个参数'aboutTemplate'与name参数有什么不同 - name:'aboutTemplate'?

任何帮助表示赞赏

1 个答案:

答案 0 :(得分:2)

Session是一个仅限客户端的软件包,您需要一个自定义软件包(如问题评论中所述)才能在服务器上获取它。

至于Iron Router route函数的第一个参数,它用于指定参数(例如/posts/:id)并由Iron Router用来猜测一些参数,例如模板使用(Router.route('homepage')客户端将搜索homepage模板。

在这种情况下,Iron Router会猜测这个名字,所以你的代码有点多余(但在我看来更干净)。 See more in the guide.

相关问题