1

Ok this is what I've got. The collection called Posts has content and I want to publish this under the name Merchs, the find() in the publish-function finds data but that is not shared to the client where Merchs is always empty.

//shared

Merchs = new Meteor.Collection('merchs');
// Posts has data I want to publish as "Merchs"
this.Posts = new Meteor.Collection('posts');

//server

Merchs.allow({
  insert: function(userId, doc) {
    return true;
  },
  update: function(userId, doc, fields, modifier) {
    return true;
  },
  remove: function(userId, doc) {
    return true;
  }
});

Meteor.publish('merchs', function(data) {
    return Posts.find();
});

//client

Deps.autorun( function() {
    Session.get('selectedCategories');
    subs.subscribe('merchs');
});
  • In shared code, `merchs` should match the name of the collection in Mongo (so I'm guessing that's `posts` or `Posts` or similar. In client code, I think you want `Meteor.subscribe('posts')` or whatever the collection name is from above. – Michael Mason May 27 '15 at 14:02
  • Meteor.subscribe('posts') is already defined and has data saved in it. What I now want to do is publish it under a different name "Merchs". In the publish-function I want to publish data from Posts but using a different name. – particular parameters May 27 '15 at 14:23
  • But you're publishing to 'merchs' not 'posts'. Just ensure the shared code above refers to the actual _case-sensitive_ name of the **Mongo** collection you want to create a Meteor collection for. (e.g. `Merchs = new Meteor.Collection('Posts');`. Then you can just do `return Merchs.find();` in your publish function, and `Meteor.subscribe('merchs')` on the client. EDIT: Not sure if using `meteorhacks:subs-manager` changes any of that but I would imagine not. Would recommend making sure it's working with `Meteor.subscribe` first. – Michael Mason May 27 '15 at 14:27
  • That would work, I can see that. What happens then though is "Error: A method named '/posts/insert' is already defined", because Posts is already defined and used. – particular parameters May 27 '15 at 14:31

1 Answers1

1

When creating your collection, the name in parentheses should be the name of the Mongo collection.

Merchs = new Meteor.Collection('merchs');

Should be:

Merchs = new Mongo.Collection('Posts');

That is, unless you already have a Posts variable defined in code that you didn't show. If you've already defined Posts and you're just looking to make another subscription to the same collection then you don't need this line at all:

Merchs = new Meteor.Collection('merchs');

You also don't need your allow() method (you can just use the one defined for Posts). All you need is the publish() method that you defined.

On the client side you also need:

Meteor.subscribe('merchs');

Also note the use of Mongo.Collection instead of Meteor.Collection which was renamed in Meteor 0.9.1.

You might want to read this excellent answer regarding publish/subscribe: https://stackoverflow.com/a/21853298/4665459

Community
  • 1
  • 1
Mark Leiber
  • 3,118
  • 2
  • 13
  • 22
  • I am subscribing already, it's in the question. What I now want to do is publish it under a different name "Merchs". In the publish-function I want to publish data from Posts but using a different name. I've seen this done in many examples but I can't find out what I'm doing differently. – particular parameters May 27 '15 at 14:27
  • Thanks for informing us on the new Meteor.collection change. – particular parameters May 27 '15 at 14:40
  • After reading up on the stackoverflow post http://stackoverflow.com/a/21853298/4665459 I now realize what I was doing wrong. – particular parameters May 27 '15 at 17:46