异步API调用错误(Async API call error)

我目前正在尝试使用流星调用twitter API,到目前为止,我得到了这个:

updateTotalFoll:function(){ var Twit = Meteor.npmRequire('twit'); var T = new Twit({ consumer_key: 'AWzYAlWFRh9zsownZMg3', consumer_secret: 'aYpL3zMPfqRgtX1usPQpEREEXVNPfNYna9FiIwTeDYR', access_token: '4175010201-TEp9qNKO4mvjkj0GMjJFZIbGPYaVv4', access_token_secret: 'EPpcJyN27E4PvhJpYaTHflNFOv3DuR05kTP2j' }); var Id2=RandomCenas.findOne({api:"twitter"})._id; T.get('statuses/user_timeline', { screen_name: 'jeknowledge' }, function (err, data, response){ //console.log(data[0].user.followers_count); RandomCenas.update(Id2,{$set:{totalFoll:data[0].user.followers_count}}); }); }

“RandomCenas”是一个MongoDB。 我想要做的是更新这个集合与来自呼叫的信息,但我得到这个错误

Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.

我在网上搜索了一个方法来解决这个问题,但我似乎无法应用我遇到的解决方案。 有关我如何处理这个的任何帮助?

I'm currently trying to call the twitter API using meteor and so far i got this:

updateTotalFoll:function(){ var Twit = Meteor.npmRequire('twit'); var T = new Twit({ consumer_key: 'AWzYAlWFRh9zsownZMg3', consumer_secret: 'aYpL3zMPfqRgtX1usPQpEREEXVNPfNYna9FiIwTeDYR', access_token: '4175010201-TEp9qNKO4mvjkj0GMjJFZIbGPYaVv4', access_token_secret: 'EPpcJyN27E4PvhJpYaTHflNFOv3DuR05kTP2j' }); var Id2=RandomCenas.findOne({api:"twitter"})._id; T.get('statuses/user_timeline', { screen_name: 'jeknowledge' }, function (err, data, response){ //console.log(data[0].user.followers_count); RandomCenas.update(Id2,{$set:{totalFoll:data[0].user.followers_count}}); }); }

with "RandomCenas" being a MongoDB. What i'm trying to do is updating this collection with the info from the call , but i get this error

Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.

I've searched the web for a way to counter this , but i cant seem to apply the solutions that i came across with. Any help with how i can deal with this?

最满意答案

试试看吧

T.get('statuses/user_timeline', { screen_name: 'jeknowledge' }, Meteor.bindEnvironment(function (err, data, response) { //console.log(data[0].user.followers_count); RandomCenas.update(Id2,{$set:{totalFoll:data[0].user.followers_count}}); }));

发生这种情况的原因是因为你在当前流星光纤之外发生的回调函数检查答案错误:流星代码必须始终在光纤内运行

try it like that

T.get('statuses/user_timeline', { screen_name: 'jeknowledge' }, Meteor.bindEnvironment(function (err, data, response) { //console.log(data[0].user.followers_count); RandomCenas.update(Id2,{$set:{totalFoll:data[0].user.followers_count}}); }));

the reason this is happening is because callback function which you pass it's happening outside the current Meteor's Fiber check the answer Error: Meteor code must always run within a Fiber

更多推荐