Meteor-Community-Packages / meteor-collection-hooks

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

Call server method from client hook. #107

Closed fpoirier1 closed 9 years ago

fpoirier1 commented 9 years ago

Hello,

I have this hook on the client side :

// client/notify.js
Messages.after.insert(function(userId, message){
  Meteor.call('notificationCreate', {messageId : message._id});
});

and this method on the server side :

// server/notify.js
Meteor.methods({
  'notificationCreate' : function(params){
       check(params, Object);

       // some security checks

      return Notifications.insert(params)
  }
});

The method seems to be never called. I need to have my method on the server only because there are some checks that can't be done on the client for some reasons (publications). And I need to have my hook on the client because there are some context data that needs to be send to the server.

What should I do ?

matb33 commented 9 years ago

Have you tried restructuring a bit by having your notification created in a server-defined Messages.after.insert hook? It's safer in general

nixoz commented 9 years ago

@matb33 such a great advice. I'm also dance around tried to call method from client/server hook. wrap hook itself to server only code made my day.