1

I'm trying to pass a function as an argument but for some reason this doesn't work. While debugging I end up with this situation:

with a server/twitter.js:

Meteor.methods({
  mytweets: function(callback){
    //someday I'll asynchronously get some tweets, then callback.
    console.log("server got callback=",callback)
  }
})

This works:

in /client/views/twitter.js:

Template.twitter.created = function(){
  Meteor.call("mytweets",123);
}

this properly logs server got callback=123 and at this point everything is ok. However the following won't work:

This won't work. Why?

Meteor.call('mytweets', function(){
  return 123;
})

when passing a function, the output is server got callback= undefined

Any idea why ?

note: I'm new with both meteor and javascript so I don't really know (yet) if that's more a javascript or a meteor-related issue. I'll tag both just in case.

Thanks in advance !!

aherve
  • 3,795
  • 6
  • 28
  • 41
  • functions aren't serializable and can't be passed in as an argument to the method call. It is mentioned in the documentation [Meteor.call](http://docs.meteor.com/#/full/meteor_call) – rivarolle Mar 03 '15 at 16:22

2 Answers2

1

According to Meteor.call documentation, Meteor.call expect EJSON-able Objects as arguments and finally a callback, which is not passed to the method itself. That is why mytweets doesn't receive your callback as an argument, but 123 does.

If however mytweets should be asynchronous (server-side), you would have to use a Meteor plugin. Check out meteor-sync-methods for this purpose.

ngasull
  • 4,206
  • 1
  • 22
  • 36
1

Meteor.call('method') is mainly used to call a server method from the client so you can't really pass a callback method. The last parameter to Meteor.call() is an async callback though. So this will be called once the method call is complete and that is how you can get it back. You can return data and that will be passed as the result object.

// On client
Meteor.call('methodName', input1, input2, function (error, result) {
    if (error) {
       // Handle error
    }
    console.log(result.data);
});

// On server
Meteor.methods({
    methodName: function (input1, input2) {
        // Do stuff.
       return {status: success, data: 'something here'};
    }
});
pstuart2
  • 362
  • 1
  • 5
  • 1
    I get what you are doing, but what if the `Do stuff` is itself an asynchronous function ? My original idea was to perform an async `get` while server-side, in order to show the result on page when it's done. But perhaps it not the way you do it with meteor ? – aherve Mar 03 '15 at 15:39
  • I was actually completing my answer for this purpose, feel free to give me any feedback on its usefulness for your situation – ngasull Mar 03 '15 at 15:47
  • If the `Do Stuff` is async, then you can use the `Meteor.wrapAsync` http://docs.meteor.com/#/full/meteor_wrapasync. You can see an example from this post here: http://stackoverflow.com/questions/26226583/meteor-proper-use-of-meteor-wrapasync-on-server – pstuart2 Mar 03 '15 at 16:33