Search code examples
javascriptmeteorpublishsubscribe

how to properly implement publish and subscribe in meteor js


I would like to know if my implementation of publish and subscribe are correct. Im new at meteor js please help me. If you need more information regarding to my code, i am willing to give you the other source code. I read the documentation about publish and subscribe but I didn't understand the documentation about it.

import { Mongo } from 'meteor/mongo';
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';

export const Notes = new Mongo.Collection('notes');

if(Meteor.isServer) {
  Meteor.publish('secureData', function() {
    return Notes.find({});
  });
}

if(Meteor.isClient) {
  Meteor.subscribe('secureData');
}

Solution

  • The string you pass to new Mongo.Collection(<string>) should be the same as that which you passed to publish and subscribe. Try the same thing where "notes" replaces "secureData".

    if(Meteor.isServer) {
      Meteor.publish('notes', function() {
        return Notes.find({});
      });
    }
    
    if(Meteor.isClient) {
      Meteor.subscribe('notes');
    }
    

    If you have further issues, please post examples showing how you're accessing this collection in your code.