我想将使用 Twit(npm 模块)制作的流中的每条推文插入到 Tweets 集合中,在一个方法中.
I want to insert in the Tweets collection every tweets from the stream made with Twit (npm module), inside a method.
stream: function(hashtag) { //Création du stream stream = T.stream('statuses/filter', { track: hashtag }); var currentHashtag = hashtag; // Lance le stream stream.on('tweet', function (tweet) { var userName = tweet.user.screen_name; var userTweet = tweet.text; var tweetDate = tweet.created_at; var profileImg = tweet.user.profile_image_url; console.log(userName+" a tweeté: "+userTweet+" le: "+tweetDate); console.log("======================="); Tweets.insert({user: userName, tweet: userTweet, picture: profileImg, date: tweetDate, hashtag: currentHashtag}, function(error) { if(error) console.log(error); }); }); }这是我的错误信息:[错误:Meteor 代码必须始终在 Fiber 中运行.尝试使用 Meteor.bindEnvironment 包装您传递给非 Meteor 库的回调.]
And here is my error message: [Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor Libraries with Meteor.bindEnvironment. ]
我尝试了不同的解决方案,但徒劳无功,例如将 Collection 插入包裹在 Fiber() 中,并且我尝试使用 Meteor.bindEnvironment,但始终不知道如何正确使用它.
I've tried different solutions in vain, like wrap the Collection insertion in a Fiber(), and I try to use a Meteor.bindEnvironment, but never understand how to properly use it.
你能帮我吗?
====================================================
===================================================
我已经试过了.使用此代码,我不再有任何错误,但推文未插入到我的收藏中.
I've tried this. With this code, I don't have any errors anymore, but the tweets aren't inserted in my Collection.
stream.on('tweet', Meteor.bindEnvironment(function(tweet) { var tweetToInsert = { user: tweet.user.screen_name, tweet: tweet.text, picture: tweet.user.profile_image_url, date: tweet.created_at, hashtag: currentHashtag }; console.log(tweetToInsert.user+" a tweeté: "+tweetToInsert.tweet+" le: "+tweetToInsert.date+" \n "+tweetToInsert.picture+"\n"+tweetToInsert.hashtag); console.log("======================="); Tweets.insert(tweetToInsert, function(error, result) { if(error) console.log(error); else console.log(result); }); })); 推荐答案您可以在插入时运行 Fiber:
You can run a Fiber on insert:
var Fiber = Npm.require('fibers'); Fiber(function () { Tweets.insert({user: userName, tweet: userTweet, picture: profileImg, date: tweetDate, hashtag: currentHashtag}, function(error) { if(error) console.log(error); }); }).run();更多推荐
如何在 Fiber 中插入 Collection?
发布评论