简体   繁体   English

流星-服务器端API调用,每分钟插入到mongodb中

[英]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. 我正在学习Meteor,同时尝试使用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. 我现在的目标是每分钟调用TwitchAPI,然后将json对象的一部分插入mongo数据库。 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. 由于MongoDB在_id匹配并且Twitch使用_id作为其键,因此我希望后续插入将更新现有记录,或者如果_id不存在则创建一个新记录。

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. 但是,我似乎无法使Meteor.setTimeout()函数正常工作。 The call happens when I start the app but does not continue occurring every minute. 我启动应用程序时会发生通话,但不会每分钟持续发生一次。

Here is what I have in a .js . 这是我在.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. 我进行了@richsilv和@saimeunt提到的更改,并且它起作用了。 Resulting code: 结果代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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