This is the function I want to execute every couple of seconds:
if (Meteor.isServer) {
function download() {
console.log("Entering");
var Twit = Meteor.npmRequire('twit');
var T = new Twit({
consumer_key: 'x', // API key
consumer_secret: 'x', // API secret
access_token: 'x-x',
access_token_secret: 'x'
});
T.get('search/tweets',
{
q: 'banana',
count: 100
},
function(err, data, response) {
for (var i = 0; i < data.statuses.length; i++) {
Tweets.insert({ createdAt: new Date(), twitter_id: data.statuses[i].id_str });
}
}
);
}
}
I’ve thrown everything but the kitchen sink at this for the past 4 hours and I’m stuck badly. I don’t understand why it’s so hard to accomplish this in Meteor, I must be doing something wrong.
My latest error is:
Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
So, if I have this function: How can I run it every 5 seconds? What’s the easiest way to do this?
And you have to wrap the async callback function that you pass to T.get() in Meteor.bindEnvironment as well. The idea is that you have to do that for every bit of code that you make run outside of regular Meteor-called functions (Meteor methods, publications, other regular server code), so basically anything that runs async.
And minor nitpick: Put calls to require/npmRequire outside of code that executes more than once. Import and require statements should go at the top of a file for more clarity and performance (executing only once).
I also just updated that thread, adding an async/await example. That should work fine in Meteor 1.3 as async/await will handle the Fibers stuff behind the scenes while on the server-side.