简体   繁体   中英

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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM