Some help needed for where to put ensureIndex code


#1

where should i insert the ensureIndex line of code? its for geolocation.

I tried putting it in

  • imports/ui/posts/posts.js …Uncaught Error: Can only call _ensureIndex on server collections
  • imports/startup/server/api.js …says ensureIndex is not a function
  • server/main.js …says ensureIndex is not a function

earlier it said cannot read config of undefined for this code.

Cloudinary.config({
   cloud_name  : Meteor.settings.private.cloudinary.cloud_name,
   api_key     : Meteor.settings.private.cloudinary.api_key,
   api_secret  : Meteor.settings.private.cloudinary.api_secret
});

The new Meteor directory is so very confusing and not helping in picking out errors. Because of the numerous imports, I cannot troubleshoot where the problem lies and have to do a trial and error style of solving issues. Fairly painful.

They should draw out the relationships in a diagram and explain: (I found the MeteorChef style of explaining far more useful though it doesnt cover file placements).

  • where and how to do imports …like ‘./’ ‘…/’ ‘…/…/’ or just ‘/blabla/’ etc and why?
  • what to import at main.js on both client and server and what they serve
  • where to put what files and why. the examples given in the docs are so narrow…just one case. what about security json files? what about config? what about ensureindex? what about user collection permissions? doesnt help that files have repeated same names.

Any help will be much appreciated!


#2

Put it in Meteor.startup() on the server:

// /server/startup.js (for example) ...
Meteor.startup(() => {
  SomeCollection._ensureIndex(... index definition here ...);
 // other startup code
});

Note it is _ensureIndex(), not ensureIndex().


#3

Or better yet, use createIndex:

// /server/startup.js (for example) ...
Meteor.startup(() => {
  SomeCollection.rawCollection().createIndex(fields, options);
 // other startup code
});

#4

Yep - that’s also a good option :slight_smile:


#5

Thanks everyone will bear those in mind.
For now because Im still using html and React seems pretty much overkill imo, ill stick to the old Meteor folder structure for now.

Much more intuitive, cleaner with better performance actually. Not to mention, wayyy easier to troubleshoot problems.