Sending a single document to the client? Help me avoid antipatterns


So in a situation where you only need a single document, say a post, how do you send just that document down?

Returning document.findOne(documentToFindID) in a publish call throws an error concerning requiring a cursor be sent.

I’ve thought of different ways for handling this but they all seem…I dunno. Not right. Not wrong…but not right.

Thought One:

Since I was planning on using an ID anyways, just publish the cursor with the ID parameter. It’ll only find one document to publish, then on the client call document.findOne() without an argument. Should only find a single document, the one I wanted.

This brings the risk for accidentally getting the wrong document if something else is published alongside it.

Thought Two:

Use a meteor method and return the value. Simple, sweet, the closest thing to what I want. I get my value from the server to the client, I only get what I want and need for my route/template.

But…if that document is already cached in MiniMongo I made an extra call for no reason. Once might not be a problem, but as a pattern this could get rough. I could store it in local storage or in a new Meteor.Collections(null) client only collection but I don’t know if that would really help me or not.

Thought Three:

Either add a package like findInSubscription or wrap my finders in functions with if(Meter.isServer){... nested within. This seems dirty and I’m worried it will leak code client side. My understanding is that files in a /server/ directory aren’t sent to the client, but I don’t think the meteor-apper-packager-upper thing removes code inside of isServer blocks.

Meteor makes me think of documents like potato chips…I can’t just have one.

Help me rein in my potat…document addiction.


This is pretty dependent on what your goal is. If you need the data to be reactive and you want it to update in real time then there isn’t anything wrong with cursor.find({_id:id}); That will ensure that any updates that happen to that document will be seen on the client in real time. If real time isn’t important to you then a method will work fine as well.

I will say that there are very few cases where you should call document.findOne() on the client with no query parameters. Even if it feels redundant you should probably add some search parameters so you can get the document you want every time.