markhorrocks
markhorrocks

Reputation: 1398

Iron router: how to set session using this.params._id

I have a route /scoreboard/1 where 1 is not a restful id of an object but a court id which I want to use to select games where court.

I have tried this code which sets the session but doesn't load the template. The template loads normally if I remove the event and hard code var court.

this.route('scoreboard',
    {

    path: '/scoreboard/:_id',

    template: 'scoreboard',

    onBeforeAction: function () {
       Session.set('court', this.params._id);
    }

  }); //route

I have found that this seems to work. What isn't working is this:

var court = Session.get("court");

console.log(court); -> 1

myGame = Games.findOne({court_id: court});

while this works:

myGame = Games.findOne({court_id: 1});

found it!

  var court = parseInt(Session.get('court'));

Upvotes: 4

Views: 2007

Answers (2)

Christian Fritz
Christian Fritz

Reputation: 21354

This works for me:

$ meteor create test
$ cd test
$ meteor add iron:router

test.js:

if (Meteor.isClient) {
    Template.scoreboard.id = function() {
        return Session.get('court');
    }
}

Router.route('/scoreboard/:_id', {
    name: 'scoreboard',
    path: '/scoreboard/:_id',
    template: 'scoreboard',
    onBeforeAction: function () {
       Session.set('court', this.params._id);
    }
});

test.html:

<head>
</head>

<template name="scoreboard">
  Scoreboard<br/>
  id is: {{id}}
</template>

Then go to localhost:3000/scoreboard/123.

Upvotes: 8

M&#225;rio
M&#225;rio

Reputation: 1612

For me it looks like you're comparing integers with strings and that the cause of your bug. You can quickly test it with Robomongo (or any tool related) against your database.

Upvotes: 2

Related Questions