meteorpublish-subscribemeteor-blazemeteor-publications

Publish and subscribe to a single object Meteor js


How to publish single objects seems not clear enough to me. Please what's the best way to handle this. This code snippet does not display anything on the view.

Helper file

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;
          }
       }
   }    
},

Publish file

Meteor.publish('SingleSchool', function (schoolSlug) {
  check( schoolSlug, Match.OneOf( String, null, undefined ) );
    user = Meteor.users.findOne({_id:this.userId})
  if(user) {
    if(user.emails[0].verified) {
      return SchoolDb.findOne({ slug: schoolSlug, userId: {$lt: this.userId}});
    } else {
      throw new Meteor.Error('Not authorized');
      return false;
    }
  }
});

template file

<template name="view">
  {{#if currentUser}}
    {{#if Template.subscriptionsReady }}
      {{#with singleSchool}}
        {{singleSchool._id}}
        {{singleSchool.addschoolname}}
      {{/with}}
    {{/if}}
  {{/if}}
</template>

Solution

  • As you said "This code snippet does not display anything on the view." well, inside Meteor.publish you need to return cursor, not array or any other object.

    So use this code:

    Meteor.publish('SingleSchool', function (schoolSlug) {
      check( schoolSlug, Match.OneOf( String, null, undefined ) );
      var user = Meteor.users.findOne({_id:this.userId});
      if(!user || !user.emails[0].verified) {
            throw new Meteor.Error('Not authorized');
        }
        return SchoolDb.find({ slug: schoolSlug, userId: {$lt: this.userId}},{limit:1});
    });
    

    I would definitely recommend you to go through How to avoid Common Mistakes