node.js - How to pass a session befor routing with iron:router on meteor? -


in post_item.html got line:

 <a href="{{pathfor 'postpage' _id=this._id}}" class="discuss btn">discuss</a> 

in router.js:

  router.map(function() {     this.route('postpage',{path:'/posts/:_id',     beforeroutingto: function(id) {         console.log("hello!");        session.set('currentpostid', id);      }   });   }); 

in post_page.js

template.postpage.helpers({      currentpost: function() {      return posts.findone(session.get('currentpostid'));      } }); 

in post_page.html

<template name="postpage">  {{#with currentpost}}  {{> postitem}}  {{/with}} </template> 

what doing wrong? how can pass id new 'page' , show result?

i'm assuming you're working on meteor tutorial using book 'discover meteor'. faced same issue, however, able solve without using session. don't need helpers anymore, if use iron router.

client/router.js:

router.map(function() {     this.route('/post/:_id', function () {       var item = posts.findone({_id: this.params._id});       this.render('postitem', {data: item});     },     {       name: 'post.show'     }); }); 

client/posts/post_item.html

<a href="{{pathfor route='post.show'}}" class="discuss btn">discuss</a> 

as see, in router.js id parsed url. also, name /post/:id route after 'posts.show' , can url view referencing name: {{pathfor route='post.show'}}


Comments