2
votes

I've a simple meteor application I made for learning how to do CRUD operations. It works great but I'm having issues with the publish/subscribe of my collection and I can't for the life of me figure it out.

My folder structure is

/client
    /main.js
    /templates
/public
/server
    /main.js

I've a main.js file in server folder with basic collection with a publish feature

/server/main.js

Dist = new Mongo.Collection('dist');

Meteor.publish('dist', function (){
    var currentUser = this.userId;
    return Dist.find({owner: currentUser});
});

and under client folder a subscribe

/client/main.js

Meteor.subscribe('dist');

I've a for each loop in a template that is suppose to show the returned info for the user. When my servers main.js is in the server folder my template loop returns nothing but CRUD calls back to the server work fine. If I move the server main.js file into the root of my project everything works as intended. However, doesn't this defeat the purpose of publish/subscribe aspect: keeping the main collection on the server side while showing users only their own data from it?

I can't figure out why this is happening after endless Google searches nothing seems to make a difference. Auto publish and insecure have been removed as well. The only third party package I believe is bootstrap for styling.

1

1 Answers

1
votes

You haven't created the Collection on your client. Thus, you have subscribed to it, but can't access to the data with a variable. To solve, simply put Dist = new Mongo.Collection('dist'); in a shared folder (anything except client, server, private...) or in both locations.

I think you can even launch your app as it is now, declare the collection in the browser console, and your data will be accessible from the client Dist variable.

Putting it in a shared folder allows you to write the code once and execute it everywhere.
Declaring the Collection does not automatically make it full of all the data on the server (unless you have autopublish). It just makes an end-point on the client, which you have to fill with Subscriptions and carefully written and bounded Publications.