Meteor.publish回调需要包装在Fiber中

时间:2014-01-17 09:54:11

标签: javascript node.js meteor

每当客户端重新加载/断开页面时,我都会收到以下错误。但是,当我删除涉及集合myCollection.insert(data)的代码行的这一部分时,错误消失了。

这里发生了什么?似乎没有涉及非Meteor库..

服务器/ publications.js

Meteor.publish('mySubscription', function() {

    this._session.socket.on('close', function() {
        myCollection.insert(data)    // removing this line avoids the error
    });

    return mySubscription.find();
});

错误

packages/mongo-livedata.js:3022
        throw e;                                                              
              ^
Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
    at Object.Meteor.bindEnvironment (packages/meteor/dynamics_nodejs.js:65)
    at null.<anonymous> (packages/meteor/helpers.js:108)
    at MongoConnection.(anonymous function) [as remove] (packages/mongo-livedata/mongo_driver.js:561)
    at Meteor.Collection.(anonymous function) [as remove] (packages/mongo-livedata/collection.js:447)
    at SockJSConnection.<anonymous> (app/server/publications.js:33:26)
    at SockJSConnection.EventEmitter.emit (events.js:117:20)
    at Session.didTimeout (/Users/username/.meteor/packages/livedata/ab19e493b9/npm/node_modules/sockjs/lib/transport.js:210:23)
    at WebSocketReceiver.GenericReceiver.didAbort (/Users/username/.meteor/packages/livedata/ab19e493b9/npm/node_modules/sockjs/lib/transport.js:296:35)
    at thingy_end_cb (/Users/username/.meteor/packages/livedata/ab19e493b9/npm/node_modules/sockjs/lib/transport.js:280:22)
    at EventEmitter.emit (events.js:95:17)
    => Exited with code: 8
    => Meteor server restarted

使用Meteor 0.7.0.1

1 个答案:

答案 0 :(得分:7)

.on内的回调将不再是光纤。尝试将其包裹with Meteor.bindEnvironment()

像这样:

this._session.socket.on('close', Meteor.bindEnvironment( function() {
    myCollection.insert(data)    // removing this line avoids the error
}, function( error) {console.log( error);});