How do you get documents that are outside of your initial publications?


#1

Sorry if this has already been asked before.

How do you get documents that are outside of your initial publications?

For example, let’s say I have posts with the following fields:

{
  _id: String
  password: String (optional),
  body: String,
  createdAt: Date
}

The posts with a password only have some metadata published and the posts without a password is completely published:

Meteor.publish('locked_posts', function() {
  return Posts.find({ password: { $exists: true } }, { fields: { createdAt: 1 } });
});

Meteor.publish('public_posts', function() {
  return Posts.find({ password: { $exists: false } });
});

The view looks like this:

{{ #each posts }}
  {{ #if password }}
    <input type="password">
  {{ else }}
    <div>{{ body }}</div>
  {{ /if }}
{{ /each }}

Inside a template, a user should be able to enter a post’s password and get that post’s body:

So do I re-publish that single post, for which the password was entered, with all the fields, and refresh the template to display this new post?


#2

Drop the subscription and subscribe to the locked_posts or check out the client only collections section at https://medium.com/@Dominus/meteor-tips-and-workarounds-b791151ce870


#3

If I use the client only collection technique, don’t I still expose the unwanted fields to the client, which would be easy to find?


#4

You can return multiple cursors from publication and also pass arguments.
So you can return one Posts.find where you return all but the ones from exclude_list.
In exclude_list will be these with special handling for which you have passwords.

From every item in exclude_list you will do 2 Posts.find:

  1. in case password matched
  2. in case it does not match - in that case returning same properties as anonymous one

I would try it this way.