HTTP API calls not working, result = undefined


#1

This has got to be something really simple/dumb that I’m encountering here. I think I need another pair of eyes.

So, I just have some simple client-side code:

Template.homePage.onRendered ->
  Meteor.call 'doit', 'https://api.site.com/action?key=abc123', (err, result) ->
    console.log result

And server-side:

Meteor.methods
  doit: (url) ->
    Meteor.http.get url, (err, result) ->
      console.log result.data
      return result.data

In the shell, I can see on the server side, result.data definitely exists:

I20150413-18:45:34.463(-7)? { balance: -6.66,
I20150413-18:45:34.464(-7)? pending_charges: 0.88,
I20150413-18:45:34.464(-7)? last_payment_date: ‘2015-03-30 05:24:19’,
I20150413-18:45:34.464(-7)? last_payment_amount: ‘-1.00’ }

But on the client side, result is undefined. Why? What am I doing wrong?


#2

On the server you need to use the synchronous version of HTTP.get:

Meteor.methods
  doit: (url) ->
    try
      HTTP.get(url).data
    catch
      false

Otherwise, the function will just return undefined before the callback is executed.


#3

Oops, I guess that made no sense… for an async client call to call an async server method. :slight_smile: Thanks.