kevinius
kevinius

Reputation: 4610

Meteor: Session.set, inside Router.route(), is then undefined when used in a 'client' page

I have an iron router declared in app.js which is in a root 'lib' folder.

    Router.route('/editTiki/:_id', function () {

        var params = this.params
        var id = params._id

        Session.set("editTikiID", id)

        this.render("editTikiRoute")

    }, {name:"editTikiRoute"});

Then, inside editTiki.js, which is inside the 'client' folder, i have this code:

console.log(Session.get("editTikiID"))
// undefined

However, it's undefined and don't understand why. I need it to subscribe to the right article:

Meteor.subscribe("tikiDBEdit", Session.get("editTikiID"))

thx for your insight...

Upvotes: 0

Views: 904

Answers (1)

PhilippSpo
PhilippSpo

Reputation: 789

Just subscribe in your router like here: https://github.com/EventedMind/iron-router/blob/devel/Guide.md#wait-and-ready
or even simpler if you don't want to wait until your subscription is ready:

Meteor.subscribe("tikiDBEdit", id);

The session variable won't be defined until the route was executed. Any code in your editTiki.js file that is not in a function (like your console.log I guess) will be executed before the session variable is set in your router. Thats just how javascript works.
This means that session variable should be defined in any template helper or the events and rendered functions of your template.

Upvotes: 1

Related Questions