如何在 Fiber 中插入 Collection?

编程入门 行业动态 更新时间:2024-10-24 02:31:38
本文介绍了如何在 Fiber 中插入 Collection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将使用 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?

本文发布于:2023-10-29 10:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539429.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Fiber   Collection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!