What might cause Collection.find() to not work?

This is the code im using: (its located in a ValidatedMethod function, on the server)

    console.log('Orgs:', OrganisationsDB.find({}).fetch());
    console.log('Accs:', AccountsDB.find({}).fetch());
    console.log('Users:', Users.find({}).fetch());

I can assure you that all 3 collections have data in them, as I can see them with RoboMongo. Whenever I check the console in the web browser, all of them return an array of objects, except, the Organisations one.

I also did some test example where I pretty much did this code, not only for 3 collections shown above, but for all 10 of mine, and they all printed their data, except for again, the Organisations one, which reports the array is empty.

If I query it in RoboMongo, it works, and displays the data, but not in meteor.

Oddly enough, I have this popup menu, using React Router, and if I view this “popup page” on top of another page, Organisations prints its data. However, if I view this page alone, it doesn’t work.

Also, I doubt its a subscription problem, as I can load data from other collections that have absolutely no relation to this popup page, but 2/3 collections related to this popup page work, the 1/3 being the Organisations.

Note that my Organisations Collection works everywhere else except this one page, weird.

What might cause this?

Don’t take this the wrong way, but if 2/3 are working, I don’t think it’s an issue with the .find(). Sounds like either a subscription issue (in your non-working page versus your working popup page), or a publication issue. Are you using publish composite?

Anyway, if I was trying to debug this, I would try logging my pubs first:

Meteor.publish('orgs', function() {
  // Your publish code
  //
  console.log(orgs.fetch());
  return orgs;
});

Do you see the expected data there? If you do, you know it’s an issue with your sub.

2 Likes

There’s also this for Chrome:

with it you can see easily whether your subscriptions work or not - simply switch to the tab MiniMongo.

1 Like

Thanks for the replies. I actually did some more tinkering around and figured out that I needed to have a subscription to the db. Odd, as I thought that the server sided code did not need it. It seems that I have existing components in my page which all subscribe to various collections, and you can guess which one did not have a subscription (thus it didn’t work).

So it is definitely confirmed that a subscription to the collection was missing.

I’ll definitely checkout that meteor dev tools, it seems like it would make life easier, like the react plugin for chrome.

If you are using validated-method your run method is executed on the client and the server. That’s most likely why you need a subscription: the method runs on the client first as a simulation. The workaround for this is to enclose the innards of the run method in a Meteor.isServer block:

.run(options} {
  if (Meteor.isServer) {
    // code here
  }
});