简体   繁体   中英

Meteor - Server-side API call and insert into mongodb every minute

I am in the process of learning Meteor while at the same time experimenting with the TwitchTV API.

My goal right now is to call the TwitchAPI every minute and then insert part of the json object into the mongo database. Since MongoDB matches on _id and Twitch uses _id as its key I am hoping subsequent inserts will either update existing records or create a new one if the _id doesnt exist yet.

The call and insert (at least the initial one) seem to be working fine. However, I can't seem to get the Meteor.setTimeout() function to work. The call happens when I start the app but does not continue occurring every minute.

Here is what I have in a .js . file in my server folder:

Meteor.methods({
    getStreams: function() {
        this.unblock();
        var url = 'https://api.twitch.tv/kraken/streams?limit=3';
        return Meteor.http.get(url);
    },
    saveStreams: function() {
        Meteor.call('getStreams', function(err, res) {
            var data = res.data;

            Test.insert(data);
        }
    }
});

Deps.autorun(function(){
    Meteor.setTimeout(function(){Meteor.call('saveStreams');}, 1000);
});

Any help or advice is appreciated.

I made the changes mentioned by @richsilv and @saimeunt and it worked. Resulting code:

Meteor.startup(function(){
    Meteor.setInterval(function(){Meteor.call('saveStreams');}, 1000);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM