DEfusion
DEfusion

Reputation: 5613

Iron router data findOne returning undefined

I have a streams publication and subscription setup but for some reason if I do the following in my route the view gets no data:

Router.route '/seasons/:season/episodes/:episode',
    name: 'episode'
    action: ->
        @render(
            'episode',
            data: ->
                Streams.findOne({season: @params.season, episode: @params.episode})
        )

If I log the params they are there as expected, and doing a findOne manually either via the db or the browser console returns the data as expected.

If I remove the params so it just does Streams.findOne() the data returns the first stream from the database and the view has access to it as expected. I'm really not sure what's going on here.

Upvotes: 1

Views: 241

Answers (1)

saimeunt
saimeunt

Reputation: 22696

You probably need to wait on your streams publication before trying to access the data : Pub/Sub mechanism in Meteor is asynchronous, when you subscribe to some data, you don't instantly get it back in the browser because of the underlying client/server latency.

Try reorganizing your code as follow :

Router.route '/seasons/:season/episodes/:episode',
  name: 'episode'
  template: 'episode'
  data: ->
    Streams.findOne({season: @params.season, episode: @params.episode})
  waitOn: ->
    Meteor.subscribe 'streams', @params.season, @params.episode

Upvotes: 1

Related Questions