0

I am experiencing this weird scenario that I am unable to figure out what the problem is. There is a pagination for a collection which works fine when navigating. I have 5 documents in a collection with each to display per 2 on a page sing the pagination. Each document has a url link that when clicked it displays the full page for the document.

The challenge now is that if I click a document on the first page, it displays the full record, but if I navigate to the next page and click a document, it displays a blank page. I have tried all I could but haven't gotten what is to be made right.

These earlier posts are a build up to this present one: Publish and subscribe to a single object Meteor js, Meteor js custom pagination.

This is the helper

singleSchool: function () {
  if (Meteor.userId()) {
     let myslug = FlowRouter.getParam('myslug');
       var subValues = Meteor.subscribe('SingleSchool', myslug );
       if (myslug ) {
       let Schools = SchoolDb.findOne({slug: myslug});
       if (Schools && subValues.ready()) {
          return Schools;
       }
    }
  }
},

This is the blaze template

<template name="view">
  {{#if currentUser}}
    {{#if Template.subscriptionsReady }}
      {{#with singleSchool}}
        {{singleSchool._id}}
        {{singleSchool.addschoolname}}
      {{/with}}
    {{/if}}
  {{/if}}
</template>
halfer
  • 19,824
  • 17
  • 99
  • 186
ken4ward
  • 2,246
  • 5
  • 49
  • 89
  • can you please put `console.log('GOt Slug');` just after line `let myslug = FlowRouter.getParam('myslug');` let us know if slug is picked up. – Ankur Soni Sep 26 '17 at 13:51
  • Thanks. I'll . BRB – ken4ward Sep 26 '17 at 13:53
  • Yes. Confirmed. It is seeing the slug. Printed on the console. – ken4ward Sep 26 '17 at 13:55
  • also why are you subscribing inside a helper. Since your `slug` is dynamic, as @Jankapunkt suggest in previous question, you must define a ReactiveVar/ ReactiveDict to get your Subscription dynamic. For that you can use `this.autorun(()=>{//subscribe here using reactive-var});` inside `Template.view.onCreated(function{//autorun here});` – Ankur Soni Sep 26 '17 at 13:56

1 Answers1

0

try this;

onCreated function:

Template.view.onCreated(function(){
    this.dynamicSlug = new ReactiveVar("");
    this.autorun(()=>{
        // When `myslug` changes, subscription will change dynamically.
        this.dynamicSlug.set(FlowRouter.getParam('myslug'));
        Meteor.subcribe('SingleSchool', this.dynamicSlug.get());
    });
});

Helper

Template.view.helpers({
    singleSchool(){
        if (Meteor.userId()) {
            let school = SchoolDb.findOne({slug: Template.instance().dynamicSlug.get()});
            if (school) {
              return school;
            }
        }
    }
});
Ankur Soni
  • 5,725
  • 5
  • 50
  • 81
  • I got this error now after implementing this: `Exception in defer callback: view.js/<@http://localhost:3000/app/app.js?hash=aa85b826ff10ef9142c8b8a7b8496905df96af6d:1087:9 viewAutorun/<@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1934:18 Template._withTemplateInstanceFunc@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3744:` – ken4ward Sep 26 '17 at 14:19
  • The autorun call is what I can decipher it is pointing to as the line where the error is. – ken4ward Sep 26 '17 at 14:26
  • ok ! btw you have to click this link `<@http://localhost:3000/app/app.js?hash=aa85b826ff‌​10ef9142c8b8a7b84969‌​05df96af6d:1087:9` it will show error line. – Ankur Soni Sep 26 '17 at 14:27