HI All,
I’m new to Meteor and been trying to implement some easy prototypes.
One of them involves calling a RESTful api that returns JSON objects. I was successful to have the server logging to the console all the results, but was never able to have the client access such data. I read all posts about sync and async calls and how to send that data.
Nothing worked… I tried HTTP.get() method with and without callback function and the same on the client Meteor.call() with and without call back function. The server always works.
Client never saw the data. I finally gave up and persisted the data on a Mongo collection and bingo… My client finally saw that. But, I was really hoping for a way to pass that data without meddling with Mongo, after all it’s meant to be transient data…
Here’s my latest try before I implemented mongo: (It even makes use of fibers/future library to help with sync calls.
Like I said before, I also tried without any function callback, but didn’t work either…
if (Meteor.isClient) {
Template.body.helpers({
getLatest: function () {
console.log("Trying to get media from client");
// Meteor.call("retrieveMedia", function(error, r) {
// if (!error) {
// console.log("returning " + r + " objects");
// return r;
// } else {
// console.log(error);
// }
try {
var r = Meteor.call("retrieveMedia");
console.log(" retrieved " + r);
return r;
} catch (e) {
console.log("caught exception " + e);
}
}
});
}
if (Meteor.isServer) {
}
Meteor.methods({
retrieveMedia: function () {
console.log("retrieveMedial called!");
if (Meteor.isServer) {
var Future = Npm.require('fibers/future');
var media = new Future();
this.unblock();
var r = HTTP.get("http://tools.cdc.gov/api/v2/resources/media?max=3", function (error, result) {
if(error) {
console.log('http get FAILED!');
media.throw(error);
}
else {
//console.log('http get SUCCES');
if (result.statusCode === 200) {
//console.log('Status code = 200!');
//console.log(result.data.results[0]);
media.return(result.data.results);
}
}
});
media.wait();
};
// return r.data.results;
}
});
Any help will be greatly appreciated as to how to fix this code.
thanks,
Marcelo.