Basic API call with Meteor

Hi,

I’m learning Meteor and am trying to get a basic API call to work. I get a 200 status code, but no routes.

On the server side I have:


var url = "http://example.com";

Meteor.methods({
    callFltAware: function() {
        this.unblock();
        return HTTP.get(url, {
        auth: "user:key",
    query: {"origin": "x", "destination": "x"} 
        });
    }
});

And on the client side:


Template.airportForm.events({
    "click input": function() {
    callFltAware();
    }
})

Meteor.call("callFltAware", function(e,r) {
	if (!e && r) {
		var plans = JSON.parse(r.content);
		console.log(plans.RoutesBetweenAirportsResult.data);
	}
})

On the console I get:

Object {statusCode: 200, content: “{“RoutesBetweenAirportsResult”:{“data”:]}}↵”, headers: Object, data: Object}
content: “{“RoutesBetweenAirportsResult”:{“data”:]}}↵”
data: Object
RoutesBetweenAirportsResult: Object
data: Array[0]
length: 0
proto: Array[0]
proto: Object
proto: Object
headers: Object
statusCode: 200
proto: Object

The data array is empty. I’m new to web dev, so maybe this is a simple fix.

Recent routes might not always be available between two arbitrary airports. Try some other locations, preferable both in the U.S. to first establish that your code is working. For example:

flightxml.flightaware.com/json/F … ation=KAUS

{“RoutesBetweenAirportsResult”:{“data”:{“count”:10,“route”:“IDU5 IDU WLEEE BITER6”,“filedAltitude”:100},{“count”:2,“route”:“IDU5 IDU CWK AUS”,“filedAltitude”:60},{“count”:2,“route”:“IDU5 IDU CWK”,“filedAltitude”:40}]}}

Thanks. I’ve tried numerous combinations, but the nothing turns up. I get the RoutesBetweenAirportsResult object so it’s partially working, but the data array is always [0].

A similar call using just Node.js works fine:


var http = require('http');
var rest = require('restler');

var fxml_url = 'http://flightxml.flightaware.com/json/FlightXML2/';
var username = 'user';
var apiKey = 'key';

var origin = 'KIAD';
var destination = 'KORD';


rest.get(fxml_url + 'RoutesBetweenAirports', {
	username: username,
	password: apiKey,
	query: {origin: origin, destination: destination}
}).on('success', function(result, resp) {
	console.log('Filed routes between ' + origin + ' and ' + destination + ':');
	var r = result.RoutesBetweenAirportsResult.data;
	for (var i = 0; i < r.length; i++) {
		console.log('Route: ' + r*.route + '
' +
			'Altitude: ' + r*.filedAltitude);
		};
	})

**

It works with Node.js, so the problem is just with your Meteor implementation and not our server side.

You might try changing how you are logging to the console so that the output is actually a deep, serialized representation of everything, rather than just the top-level of things, before printing just “Object” or “Array”. The data might actually be there, but just not logged properly to your console.

I ended up getting it working by taking the arguments out of the query and putting them directly into the URL. Probably not the most elegant solution but ok for now:


Meteor.methods({
	callFltAware: function(origin, destination) {
		this.unblock()
      try {
        var result = HTTP.call("GET", url + "?origin=" + origin + "&destination=" + destination, {
    		auth: "user:pass", 
    });
        return result
      } catch (e) {
        console && console.log && console.log('Exception calling', url)
        throw e
      }
    }
  })