Publication not Working


#1

I’m a bit of a noob and having a bit of trouble getting my publications to work. In my data, I have a number of patients and would like to show the data of a single patient. This is how I have structured my publication:

Meteor.publish('patients.single', function (patientId) {
    check(patientId, String);
    return Patients.find({_id: patientId});
});

and this is how I have subscribed:

Router.route('/patients/:_id', {
    layoutTemplate: 'ApplicationLayout',
    yieldRegions: {
        'single_patient': {to: 'content'}
    },
    subscriptions: function () {
    return Meteor.subscribe('patients.single', this.params._id);
    }
});

I have also tried to subscribe via the actual template onCreated using this.subscribe('patient.single, patientId) to no avail.

Publications seem easy in theory, but I just can’t seem to get them right. What am I doing wrong here?


#2

In onCreated, where do you get the patientId from?
You would normally set it in ‘data’ from the router

Router.route('/patients/:_id', {
    layoutTemplate: 'ApplicationLayout',
    yieldRegions: {
        'single_patient': {to: 'content'}
    },
    data: function(){
            return {pattientid: this.params._id}
        }

subscription would be like

this.subscribe(‘patients.single’, this.data.patientId);

Alternative, which should be the easiest, is to change your function name ‘subscriptions’ to ‘waitOn’ in the router


#3

I’ve tried both solutions but neither seems to be working.

Just to recap where I am now, this is my publish:

Meteor.publish('patients.single', function (patientId) {
    check(patientId, String);
    return Patients.find({_id: patientId});
});

And this is my route (using iron-router):

Router.route('/patients/:_id', {
    layoutTemplate: 'ApplicationLayout',
    yieldRegions: {
        'patientsSingle': { to: 'content' }
    },
    waitOn: function () {
        return Meteor.subscribe('patients.single', this.params._id);
    }
});

And this is my subscription:

Template.patientsSingle.helpers({
    patient: function () {
        return Patients.find({_id: Session.get('currentPatient')});
    }
});

Which I then go ahead to use like this:

{{#with patient}}
   {{ profile.surname }}, {{ profile.first_name }} {{ profile.middle_name }}
{{/with}}

Is this what you meant?