【问题标题】:How can I implement a call to a package that doesn't use fibers from with Meteor?如何通过 Meteor 实现对不使用光纤的包的调用?
【发布时间】:2012-05-15 01:52:05
【问题描述】:

我正在尝试在 Meteor 中使用 twit 以便与 Twitter REST api 通信。

如果我自己调用它,它可以在 /server/ 目录中的 server.js 文件中正常工作。如果我从一个观察中包装或调用它,甚至调用一个从观察中调用 twit 函数的函数,我会得到错误。

例如,这在 /server/server.js 中工作得非常好。

T.post('statuses/update', { status: 'hello world!' }, function(err, reply) {
  console.log('error: ' + JSON.stringify(err,0,4));
  console.log('reply: ' + JSON.stringify(reply,0,4));
});

但是,假设我想说每次插入记录时都调用 Twitter。

var query = Posts.find({}, {fields: {}});

var handle = query.observe({
added: function(post, before_index){
    if(post.twitter_id_str === undefined || post.twitter_id_str === '' ||
        post.twitter_id_str === null) { 

        T.post('statuses/update', { status: 'hello world!' }, function(err, reply) {
          console.log('error: ' + JSON.stringify(err,0,4));
          console.log('reply: ' + JSON.stringify(reply,0,4));
          if(reply){
            // TODO update record with twitter id_str
                            //  BREAKS here - crash, restart
            console.log('incoming twitter string: ' + reply.id_str);
            Posts.update(
                {_id: post._id},
                {$set:{twitter_id_str:reply.id_str}}
            );
        }
});
    } else {
        console.log('remove me we have it: ' + post.twitter_id_str);
    }   
}
});

这会引发此错误,服务器崩溃并重新启动,但在我注释了 Break 的地方没有运行代码逻辑。

app/packages/mongo-livedata/collection.js:215
        throw e;
          ^
Error: Meteor code must always run within a Fiber
    at [object Object].get (app/packages/meteor/dynamics_nodejs.js:14:15)
    at [object Object]._maybeBeginWrite (app/packages/mongo-livedata/mongo_driver.js:68:41)
    at [object Object].update (app/packages/mongo-livedata/mongo_driver.js:191:20)
    at [object Object].update (app/packages/mongo-livedata/collection.js:203:32)
    at app/server/server.js:39:13
    at /usr/lib/meteor/lib/node_modules/twit/lib/oarequest.js:85:16
    at passBackControl (/usr/lib/meteor/lib/node_modules/twit/node_modules/oauth/lib/oauth.js:359:11)
    at IncomingMessage.<anonymous> (/usr/lib/meteor/lib/node_modules/twit/node_modules/oauth/lib/oauth.js:378:9)
    at IncomingMessage.emit (events.js:88:20)
    at HTTPParser.onMessageComplete (http.js:137:23)
Exited with code: 1

总而言之,Twitter 代码本身运行良好,但在 Meteor 纤维材料中时就不行了。我尝试将它放在另一个函数中并从观察等中调用它......无济于事。

有什么建议或想法吗?

【问题讨论】:

    标签: meteor


    【解决方案1】:

    您需要在 Fiber 中执行 twit post API 调用:

    Fiber(function() { ... 你的 twit API 调用 ... }).run()

    看看这个相关的问题:"Meteor code must always run within a Fiber" when calling Collection.insert on server

    【讨论】:

    • 谢谢伙计。我在该线程中添加了对我的解决方案有用的内容。希望它会帮助其他人。
    猜你喜欢
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 2015-12-21
    • 2012-12-11
    • 2013-07-30
    • 2016-11-27
    • 2015-08-22
    • 1970-01-01
    相关资源
    最近更新 更多