Meteor-Community-Packages / meteor-collection-hooks

Meteor Collection Hooks
https://atmospherejs.com/matb33/collection-hooks
MIT License
657 stars 92 forks source link

Collection hooks defined in server folder #28

Closed wI2L closed 10 years ago

wI2L commented 10 years ago

Hello..

I am currently trying to use an "afterInsert" hook on one of my collections.

This collection is defined in the folder models/collections/.

The folder models/ also contains another folder named schemas, used to store "Simple-Schema" schemas. This is not revelant here, but i just want to precise, because its usual to place collections files in collections/

Because i want my collection hook to be exectuted on the server, i wrote it into a file which is stored in server/collections/hooks/. I could've add Meteor.isServer() after the definition of my collection, but even if this code would be only executed by the server, i understood that it would be sent to the client.

Doing everything like that, i got an erreor at meteor startup (full stacktrace) : ReferenceError: VServers is not defined where VServers if of course my collection name.

I ensure you i dont use the keyword var to define my collection.

Any idea of how i can do things ?

Meteor Collection Hooks mainpage says, "Works across both client, server or a mix", but i would like it to work only on the server, i do not trust the user for the operation that will be performed by the hook.

Regards,

William

matb33 commented 10 years ago

It sounds like a loading order issue. Your hook definition may be getting read before your collections gets a chance to be defined

wI2L commented 10 years ago

I was thinking of that indeed. I tried to rename my models/ to zodels/, to see if its related to an alphabetical loading order in the root app folder, but it didnt resolved the problem.

My publications functions are also in the server/ directory, and i never got this error in the past because of that.

I also tried to put the required file in a sub lib/ folder inside server/.

wI2L commented 10 years ago

Ok. I found the solution with this post.. https://groups.google.com/forum/#!topic/meteor-talk/3TgKLYHrbNg I just put my hooks definitions inside a sudirectory called server in my models/ directory.

Hook definition is not sent to the client, so the behaviour is ok.