Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should Meteor.methods() be defined?

Tags:

meteor

http://docs.meteor.com/#meteor_methods

I have tried it in publish.js in my server folder.

I am successfully calling Meteor.apply and attempting the server call from the client. I always get an undefined response.

like image 267
Josh Petitt Avatar asked Apr 14 '12 17:04

Josh Petitt


1 Answers

Calling Meteor.methods on the server is correct. That will define remote methods that run in the privileged environment and return results to the client. To return a normal result, just call return from your method function with some JSON value. To signal an error, throw a Meteor.Error.

On the client, Meteor.apply always returns undefined, because the method call is asynchronous. If you want the return value of the method, the last argument to apply should be a callback, which will be passed two arguments: error and result, in the typical async callback style.

Is your server code actually getting called? You can check that by updating the DB in the method and seeing if the client's cache gets the new data, or calling console.log from inside the method body and looking at the output of the "meteor" process in your terminal.

like image 160
debergalis Avatar answered Sep 21 '22 05:09

debergalis