ken4ward
ken4ward

Reputation: 2296

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>

Upvotes: 1

Views: 753

Answers (2)

Ankur Soni
Ankur Soni

Reputation: 6008

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

Upvotes: 4

Jankapunkt
Jankapunkt

Reputation: 8413

When I am concerned only for a single object, I implement this using a meteor method:

Meteor.methods({
    "getSingleSchool":function(schoolSlug) {
        //... check args and user permissions
        return SchoolDb.findOne({ slug: schoolSlug, userId: {$lt: this.userId}});
    },
});

Then in the template I run this method in the onCreated autorun part:

Template.view.onCreated(function(){
    const instance = this;
    instance.state = new ReactiveDict();

    instance.autorun(function(){
        let my slug = FlowRouter.getParam('myslug');

        // load if no loaded yet
        if (my slug && !instance.state.get("singleSchool")) {
            Meteor.call("getSingleSchool", mySlug, function(err, res){
                //handle err if occurred...
                this.state.set("singleSchool", res);
            }.bind(instance)); //make instance available
        } 

    });
});

The helper then just returns a value, if the school is loaded:

singleSchool: function () {
   return Template.instance().state.get("singleSchool");   
},

Upvotes: 1

Related Questions