Alain Goldman
Alain Goldman

Reputation: 2908

Meteor publish and subscribe with routes

Hey I'm trying to setup a link show page in meteor but the data never gets to the view. This is pretty run of the mill but I couldn't seem to get it after I removed autopublish, any ideas?

Thanks

iron router file

Router.route('/show/:_id',{
    template: "show",
    name: "show",
    data: function(){
      return Links.findOne({_id: this.params._id});
    },    
    subscriptions: function(){
      return Meteor.subscribe('links', this.params._id);
    }
});

server side

Meteor.startup(function() {

    Meteor.publish('links', function(currentLink){
      return Links.find({ _id: currentLink })
    });

});

template

<template name="show">
  {{info}}
</template>

collection

Links = new Mongo.Collection("links");

Upvotes: 1

Views: 254

Answers (2)

sbeliv01
sbeliv01

Reputation: 11810

In your router, try using the waitOn parameter. http://iron-meteor.github.io/iron-router/#the-waiton-option

Router.route('/show/:_id',{
    template: "show",
    name: "show",
    data: function(){
      return Links.findOne({_id: this.params._id});
    },    
    waitOn: function(){
      return Meteor.subscribe('links', this.params._id);
    }
});

Upvotes: 3

Brett McLain
Brett McLain

Reputation: 2010

I prefer using helpers rather than the data function. Try using a helper to connect your collection to your template. {{info}} will invoke the info helper in this code below which returns all objects in the Links collection:

Template.show.helpers({
    info: function() {
        return Links.find().fetch();
    }
});

What does your Link object look like? Does it have an "info" field?

Upvotes: 1

Related Questions