How can I can find a fixture item by id on the client side?


#1

I want to create a demo app in which, on startup, if no docs exist in a collection, a single doc is created that the user can play with. I cannot allow multiple instances to be created (for the purposes of the demo.)

However, while I can easily create the item on the server side, it is not clear how I can find it on the client side. For example:

if(Meteor.isServer){
 if(Items.find().count() === 0){
   item = Items.insert({title: "My Item"});
 }
}

How can I now locate this item by id on the client side?

I have tried using export/import, but I get undefined when I try to import from the server to the client.


#2

Do you need to find by id? You could find by anything.

If you do, then you could just insert the document with a known id:

item = Items.insert({_id: "myknownid", title: "My Item"});

#3

So funny, that is exactly the type of solution I was considering this morning.
However, I did not know that Mongo allows for manually defining the _id value.
Will give that a shot. Thx!


#4

If you’re only allowing a single item to exist in the Items collection, you could always just do a Items.findOne() on the client. That’s assuming that you’re either publishing the document, or using autopublish.