在Meteor.js中使用NPM包时,必须在光纤错误中运行



我正在使用NPM OAuth库,因为我无法让Meteor工作。我得到这个错误。

错误:Meteor代码必须始终在光纤中运行。尝试使用Meteor.bindEnvironment.包装传递给非Meteor库的回调

如果我删除Collection.unsert行,它就会起作用。

var OAuth = Meteor.require('oauth').OAuth;
var oa = new OAuth(null, null, consumer_key, consumer_secret, "1.0", null, "HMAC-SHA1");
var request = oa.post("https://stream.api.com/blah.json", access_token, access_secret);
request.on('response', function (response) {
    response.setEncoding('utf8');
    response.on('data', function(data) {
        var j = JSON.parse(data)
        Collection.upsert({symbol: j.symbol}, {last: j.last})
    })
});
request.end();

我读过关于Meteor.indEnvironment和Meteor的文章_wrapAsync,但无法使其工作。

Collection.upsert方法在嵌入到Fiber中时工作,而request.on的回调是任意调用的,而不是用一个来包装。试试这个:

request.on('response', function (response) {
    new Fiber(function(){
        response.setEncoding('utf8');
        response.on('data', function(data) {
            var j = JSON.parse(data)
            Collection.upsert({symbol: j.symbol}, {last: j.last})
        });
    }).run();
});

最新更新