Reputation: 2395
Ok so i am using a method to make a request and pull some tables from another URL
Meteor.methods({
gimmetitle: function () {
var url = 'http://wiki.warthunder.com/index.php?title=B-17G_Flying_Fortress';
request(url, function(err, response, body) {
$ = cheerio.load(body);
var text = $('.flight-parameters td').text();
console.log(text);
return text;
});
}
});
When called the td's in the table succesfully print to the server console: http://prntscr.com/721pjh
Buuut, when that text is returned from that method to this client code, undefined
is printed to the console:
Template.title.events({
'click #thebutton': function () {
Meteor.call('gimmetitle', function(error, result){
Session.set('gogle', result);
});
var avar = Session.get('gogle');
console.log(avar);
}
});
Ideas?
Upvotes: 1
Views: 112
Reputation: 2395
Quick update..Was able to fix this with just 1 line of code lol.
instead of request(url, function(err, response, body)
i used the froatsnook:request package and used var result = request.getSync(url, {encoding: null});
and then just replaced $ = cheerio.load(body);
with $ = cheerio.load(result.body);
.
Upvotes: 0
Reputation: 22696
You need to understand two different things here :
This is why Session.set('gogle', result);
is actually executed AFTER var avar = Session.get('gogle');
even though it appears before in your event handler code flow.
Session
variable to the result of the method, the event handler code is not automatically reexecuted with the new value of Session.get('gogle')
.You'll need to either do something with the result right in the Meteor method callback, or use a reactive computation (template helpers or Tracker.autorun) depending on Session.get('gogle')
to rerun whenever the reactive data source is modified, and use the new value fetched from the server and assigned to the Session
variable.
Upvotes: 1