iron-router data is not accessible inside JS
-
21-12-2019 - |
Question
My template is rendering data OK without any problem, but if I try to access to it inside my JS script i get null or undefined... Here is my code
iron-router
@route 'responder',
path: '/quesabesde/:_screenName'
yieldTemplates:
responderContent:
to: "mainContent"
responderHeader:
to: "mainHeader"
waitOn: ->
@subscribe 'getPreguntas', @params._screenName
data: ->
Preguntas.findOne({owner: @params._screenName})
Template script with NULL result
Template.responderContent.rendered = ->
console.log(@data)
Template HTML WORKING
{{#each level}}
<h1>{{title}}</h1>
{{/each}}
And I get null
on console... but as I said my template is rendering it OK. I want to access data inside my script in order to set sessions and manipulate the data for other purposes
Solution
This is a documented (if often undesirable) phenomenon. I would highly recommend reading this, particularly from Nov 19 downwards. Note that the easiest fix is just to have a loading template.
OTHER TIPS
Use this.data not just data, this is an instance of your template in the rendered callback
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow