Async function server method using YQL npm module


#1

Hello everybody,

I have a little problem regarding the YQL npm module which I want to call as a server side method to return its response to the client. I tried to wrap the query exec function with a async wrapp but I keep getting an error that it can’t find a function in the YQL class. --> in YQL.prototype.exec method -> this.getUrl -> which belongs to the YQL class but somehow its loses the context and can’t find it.

checkLocation : function(loc){
	check(loc, String);
	var YQL = Meteor.npmRequire('yql');
	var query = new YQL('select * from weather.bylocation where location="'+loc+'" and unit="c"');
            var wrap = Meteor.wrapAsync(query.exec);

	var result = new wrap(function(err, data) {
		var forecasts = data.query.results.weather.rss.channel.item.forecast;
		return forecasts;
	});
	return result;
}

everything works great this way ->

query.exec(function(err, data) {
	var forecasts = data.query.results.weather.rss.channel.item.forecast;
	// do stuff here  its good in a cron job 
});

I am a little clueless right now. Maybe there is a different way do this kind of thing.


#2

Ok I am a little closer, I tried using the meteorhacks:async package and I was able to resolve the function not found error.

checkLocation : function(loc){
	check(loc, String);
	var YQL = Meteor.npmRequire('yql');
	var query = new YQL('select * from weather.bylocation where location="'+loc+'" and unit="c"');

	var wrapped = Async.wrap(query,['exec']);

	var result = wrapped.exec(function(err, data) {
		console.log(data);
		throw new Meteor.Error('doesnt-work');
		//var forecasts = data.query.results.weather.rss.channel.item.forecast;
	});

	console.log('hmmmm',result,'');
	return result;
}

the call runs but I am not reaching the second console.log in my method.


#3

I think probably you should study this page : Tracker Manual

You have identified a section of code in which everything works …

query.exec(function(err, data) {
  var forecasts = data.query.results.weather.rss.channel.item.forecast;
  // do stuff here  its good in a cron job 
});

It is a data source for the rest of your app, right?

You ought to trust Tracker to refresh every place in your app that references “forecasts” for you , automatically.

It’s an incredibly powerful way to code.

… instead of attempting to model all interactions in a single piece of cohesive code the programmer can express what should happen upon specific changes. The paradigm of responding to a change is simpler to understand than modeling which changes affect the state of the program explicitly.


Is it true that Tracker is client-side only?
#4

I have written it so yeah I know it works it just doesn’t work in a synchronous context. As far as I know Tracker is not supported on the server but
I have figured out my problem ->

	checkLocation : function(loc){
	check(loc, String);
	var YQL = Meteor.npmRequire('yql');
	var query = new YQL('select * from weather.bylocation where location="'+loc+'" and unit="c"');
	var wrapped = Async.wrap(query,['exec']);
	return wrapped.exec();
}

now the response gets properly returned in a synchronous way.
@warehouseman the exec call does a async call to a yahoo service called yql which can return all sorts of useful information. Its not a reactive datasource. I could do something reactive via doing a round trip to a mongo datasource. You can store the result in a search collection and than publish it. That could work in a reactive way but without tracker functionality on the server I don’t see a different way.